5
$\begingroup$

I want a convenient way to save lots of intermediate data lists into files, so that next time the files can be imported automatically to homonym list variables in the notebook, without having to evalutate some time-consuming intermediate steps again.

I can already use the following expression to export table1 to a file of same name.

table1 = {{1, 2}, {3, 4}};
Export[ToString@((Trace@table1)[[1]]) <> ".dat", table1]

Then I define a table-saving function in this way:

tablesaveFunction := Export[ToString@((Trace@#)[[1]]) <> ".dat", #] &
tablesaveFunction[table1]

what I get is:

Part::partw: Part 1 of {} does not exist. >>
{}[[1]].dat

I guess the problem is about the order of evaluation, but do not know how to solve.

Or, any other easy methods to achieve my goal?

$\endgroup$
4
  • $\begingroup$ Try SetAttributes[tablesaveFunction, HoldAll]. $\endgroup$
    – N.J.Evans
    Commented Jun 18, 2015 at 15:42
  • $\begingroup$ Same result.... $\endgroup$
    – Chromatic
    Commented Jun 18, 2015 at 16:03
  • $\begingroup$ Look up DumpSave in the documentation (and on this site)... $\endgroup$ Commented Jun 18, 2015 at 16:09
  • 2
    $\begingroup$ Remove[tablesaveFunction]; SetAttributes[tablesaveFunction, HoldAll]; tablesaveFunction[x_] := Export[ToString@#1 <> ".dat", #2] &@@x; works for me, but you end up writing the table with the head HoldForm which may not be acceptable. $\endgroup$
    – N.J.Evans
    Commented Jun 18, 2015 at 16:16

2 Answers 2

5
$\begingroup$

You need to set a Hold attribute for your function, as @N.J.Evans mentioned as well. Also, instead of using Trace to obtain the symbol name, try using SymbolName instead:

Clear[exportTable]

SetAttributes[exportTable, HoldFirst]
exportTable[t_] := Export[SymbolName[Unevaluated[t]] <> ".dat", t]

exportTable[table1]

You can then re-import your table at will:

Import["table1.dat"]
(* Out: {{1, 2}, {3, 4}} *)
$\endgroup$
3
  • $\begingroup$ This works, though I may use DumpSave, thanks to @MikeLimaOscar $\endgroup$
    – Chromatic
    Commented Jun 18, 2015 at 17:02
  • $\begingroup$ @Chromatic I think DumpSave would indeed be a good idea especially if your tables are massive. Nevertheless, it's probably still better to use the SymbolName approach, rather than Trace, to get to the symbol's name. $\endgroup$
    – MarcoB
    Commented Jun 18, 2015 at 17:19
  • $\begingroup$ @Chromatic, if you think that my answer above adequately addresses your problem, would you consider formally accepting it by clicking on the grey check mark next to it? $\endgroup$
    – MarcoB
    Commented Jul 13, 2015 at 19:04
1
$\begingroup$

Instead of starting with the symbol table1 and trying to extract the name of it and convert it to a string, why not give the string as the function argument and use ToExpression inside the function to access the variable?

exportTable[s_String] := Export[s <> ".dat", ToExpression@s]

You call this with a string with the same name as the table you want to export:

exportTable["table1"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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