Group Abstract Group Abstract

Message Boards Message Boards

0
|
526 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How to export an expression with file name equal to expression?

Posted 26 days ago

I want to create the file myplot.pdf that has the content of the expression myplot where

myplot=ListPlot[{}]

I tried

Export[ToString[myplot] <> ".pdf", myplot]

but this creates -Graphics-.pdf and not myplot.pdf. How to fix it?

I later want to create the function

Export[ToString[#]<>".pdf", #]&

that will act on several expressions myplot1, myplot2 etc. to create pdfs of several plots in my notebook.

POSTED BY: Ehud Behar
5 Replies
Posted 24 days ago

And here's another way:

SetDirectory[NotebookDirectory[]]

myPlot1 = Plot[Sin[x], {x, 0, 4 Pi}];

myPlot2 = Plot[Cos[x], {x, 0, 4 Pi}];

myPlot3 = Plot[Cos[x], {x, 0, 6 Pi}];

(*don't let exportPlot evaluate its arg*)
SetAttributes[exportPlot, HoldFirst]

(*exportPlot can be used to export any plot with its symbol name used 
for the file name*)

exportPlot[plot_] := 
 Export[SymbolName[Unevaluated[plot]] <> ".pdf", plot]

exportPlot[myPlot1]

(*to map onto list, keep the list unevaluated*)
exportPlot /@ Unevaluated[{myPlot2, myPlot3}]
POSTED BY: David Keith
Posted 23 days ago

I have a different take. Once you must type out something like

exportPlot /@ Unevaluated[{myPlot2, myPlot3}]

then it seems to me like the OP should rethink the strategy from the start. That's because you could have just as easily done this:

altExport[name_String] := Export[FileNameJoin[{filePath, name <> ".pdf"}], Symbol[name]];
altExport /@ {"myPlot2", "myPlot3"}

The point being that in all of these cases, the OP knows both the sequence of characters that form the file name and the sequence of characters that form the symbol name. It so happens that it's the same sequence of characters, but that's immaterial, and I think the OP is actually letting it be a distraction. We must literally type out the sequence of characters twice regardless. In this scenario, we have two different semantic uses for a sequence of characters: a filename and a way to reference a Mathematica expression. I just don't see any way around that. Trying to force both usages through one bottleneck just isn't necessary. It's much, much easier to just preserve the sequence of characters you want as the filename rather than to extract it later through manipulating the sequence of evaluation. Again, we're forced to type out something twice regardless, so just type those things out in a way that's convenient for how you want to use them.

One way is through an association:

plots = 
 <|"myPlot1" -> Plot[Sin[x], {x, 0, 4 Pi}], 
   "myPlot2" -> Plot[Cos[x], {x, 0, 4 Pi}], 
   "myPlot3" -> Plot[Cos[x], {x, 0, 6 Pi}]|>

Another would be with DownValues:

plots["myPlot1"] = Plot[Sin[x], {x, 0, 4 Pi}];
plots["myPlot2"] = Plot[Cos[x], {x, 0, 4 Pi}];
plots["myPlot3"] = Plot[Cos[x], {x, 0, 6 Pi}];

With either of these strategies, we keep a clean semantic separation between the sequence of characters that forms the file name and whatever the Mathematica expression is that we want saved in the file.

Having said all of that, it's entirely possible that I've misunderstood the OP's real problem. After all, if it was just saving three plots, one could just as easily do it manually. That's why I assume it's worth spending some time re-thinking the overall strategy rather than shoe-horning in special-case handling.

POSTED BY: Eric Rimbey
Posted 26 days ago

Somewhere in your code you've evaluated

myplot=ListPlot[{}]

and now you want to export that graphics object to a file whose name matches the symbol you attached the graphic to. The whole point of attaching OwnValues to a symbol is so that symbol evaluates to the thing you defined it as. So, to recover that symbol from the graphics itself is infeasible unless you've somehow embedded the symbol name into the graphic somehow, which is probably not worth trying to do.

As I read your question, it seems to me that what you want is a way to have a name attached to a graphic in such a way that you can use the name to access the graphic while also being able to extract just the name itself for other purposes. That suggests that you need some structure.

plots = <|"myplot" -> ListPlot[{}], "myotherplot" -> ListPlot[{}]|>;
KeyValueMap[Export[FileNameJoin[{theFilePath, StringJoin[#, ".pdf"]}], #2] &, plots]
POSTED BY: Eric Rimbey

You must prevent the evaluation of myplot:

Export[ToString[Unevaluated[myplot]] <> ".pdf", myplot]
POSTED BY: Gianluca Gorni

Ehud,

I would do it like so:

myplot1 = myplot2 = myplot3 = ListPlot[{}];
Export[# <> ".pdf", ToExpression[#]] & /@ {"myplot1", "myplot2", "myplot3"}
POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard