2
$\begingroup$

This seems like a simple question due to lack of experience, but I can't seem to find an answer through searching. I am trying to read back a symbol from a DumpSave:

DumpSave[NotebookDirectory[] <> "mySymbol.mx", mySymbol]

Here, using NotebookDirectory[] got my file in the current directory like I wanted, but when I try to use either of the following with Get, it fails:

<< NotebookDirectory[] <> "DiscreteTreatmentRegion.mx";
<< Evaluate[NotebookDirectory[] <> "Mysymbol.mx"];

It seems to me like it is not evaluating the filename correctly, since since it gives the message 'Cannot open ("NotebookDirectory[]").'

How can I get this to work? Or is there a better way to do what I am trying to do.

$\endgroup$
  • $\begingroup$ This << (NotebookDirectory[] <> "defs.mx") works for me in a similar situation. Try closing MMA, then loading the notebook in question and running your second line again. Obviously the notebook needs to be saved. $\endgroup$ – LLlAMnYP Dec 8 '15 at 10:13
  • $\begingroup$ StringJoin (<>) binds more loosely than Get (<<), it appears. the first example is interpreted as Get[NotebookDirectory[]] <> "file.mx", but the second line with Evaluate should work properly. $\endgroup$ – LLlAMnYP Dec 8 '15 at 10:17
4
$\begingroup$

<< is one of a few special operators which turn everything that follows into a string, without having to use quotes.

Thus << asd is just another notation for Get["asd"]. Note that the first form has no quotation marks. Illustration:

enter image description here

The answer to your problem is: use the Get form, and not <<, if the file name is computed as an expression. << is a convenient shorthand for when you type the file name directly.

Some other stringifying operators are >>, >>>, ::, and since version 10, #. #asd is the same as Slot["asd"].

This is documented under the "File Names" section here.

$\endgroup$
  • $\begingroup$ I was trying to find good reference in docs for that. Are you aware of any? ps. NotebookDirectory[] was eveluate in my case but failed to get the data anyway. $\endgroup$ – Kuba Dec 8 '15 at 11:30
  • $\begingroup$ @Kuba There's a mention under Details for Get: <<"name" is equivalent to <<name. The double quotes can be omitted if the name is of the form specified in "Operator Input Forms". $\endgroup$ – Szabolcs Dec 8 '15 at 11:31
  • $\begingroup$ I saw that but I wouldn't say it is informative. $\endgroup$ – Kuba Dec 8 '15 at 11:32
  • $\begingroup$ I see now, hmm I complain to much on docs so let me just go to say nothing more. $\endgroup$ – Kuba Dec 8 '15 at 11:35
  • $\begingroup$ @Kuba I agree. I added a reference which is a bit better. It mentions which characters can terminate the string (file name). I don't know if # is different, it probably is. $\endgroup$ – Szabolcs Dec 8 '15 at 11:35

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.