1

I am trying to use Getto import data saved as a .nb document. The problem is Getevaluates all expressions but only returns the last one. How can I return other expressions from the document?


e.g.

A={1,2,3,4,5};
B=A^2;
C=Mean[B];
Save["test",{A,B,C}];

Get["test"]

The output is 11, but I also want B to be listed in the output.


I am not able to rearrange the .nb document as it would require rerunning the code (takes several days), or manually changing the data (very tedious with~5GB worth of data). Are there any alternative functions I might be able to use?

  • 2
    What are you trying to accomplish? The values of b, etc. will be set, so they're available for use. Frankly, this seems like a bit of a bizarre way to do something. In addition, don't use uppercase initials for your symbols - it can clash with built-ins (I'm surprised you don't report an error with C - that's protected...) – ciao Aug 05 '15 at 04:48
  • With the uppercase initials I was just trying to create a simple example of the problem I have. I understand the values of b are available, however my actual data is importing several documents that use the same expression names. If I import using Get only the data (ie. b) from the last document is available. – James Reeve Aug 06 '15 at 00:02
  • I came up with this and it seems to work; Table[Get["test"<>ToSting[i]<>".nb"]; B, {i,1,rep}]. Where rep is the number of replicate runs. – James Reeve Aug 07 '15 at 00:15

1 Answers1

5

Possibly you want the Import format "HeldExpressions":

Import["test", "HeldExpressions"]
{HoldComplete[A = {1, 2, 3, 4, 5}],
 HoldComplete[B = {1, 4, 9, 16, 25}], 
 HoldComplete[Attributes[C] = {NHoldAll, Protected}]}

The last expression may not be as you expect until you remember that C is a reserved System Symbol.


Sorry, I overlooked the fact that you said you had a .nb file as your first example is a different format. Instead of Import please look at NotebookImport and try a command like:

NotebookImport[NotebookOpen @ "file.nb"]

It has a number of configuration options; please try a few of them. If you have trouble getting what you want let me know.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Import seems to be the way to go, I just don't know which format to use to return the data in a way it can be expressed. The Import can only use the formats {Data, Lines, Plaintext, String, Words} with a .nb document. – James Reeve Aug 06 '15 at 00:46
  • @JamesReeve updated. – Mr.Wizard Aug 06 '15 at 02:45
  • Thanks that should work, I just can't use the NotebookImport function on my version of Mathematica, even though I am using Mathematica 10 for Mac OS. – James Reeve Aug 07 '15 at 00:08
  • @JamesReeve Perhaps it was only introduced in 10.1? I'll try to think of another solution. – Mr.Wizard Aug 07 '15 at 01:21