1
$\begingroup$

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?

$\endgroup$
  • 2
    $\begingroup$ 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...) $\endgroup$ – ciao Aug 5 '15 at 4:48
  • $\begingroup$ Related: (60734) $\endgroup$ – Mr.Wizard Aug 5 '15 at 5:21
  • $\begingroup$ 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. $\endgroup$ – James Reeve Aug 6 '15 at 0:02
  • $\begingroup$ 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. $\endgroup$ – James Reeve Aug 7 '15 at 0:15
5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – James Reeve Aug 6 '15 at 0:46
  • $\begingroup$ @JamesReeve updated. $\endgroup$ – Mr.Wizard Aug 6 '15 at 2:45
  • $\begingroup$ 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. $\endgroup$ – James Reeve Aug 7 '15 at 0:08
  • $\begingroup$ @JamesReeve Perhaps it was only introduced in 10.1? I'll try to think of another solution. $\endgroup$ – Mr.Wizard Aug 7 '15 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.