Message Boards Message Boards

0
|
5166 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Create strings from plotNames

Posted 4 years ago

Given a list of plots, create names for the plots.

For example, create two plots, place them in a list, thread HoldForm to each item in the list, then use the list to create strings for use by Export. The following works, but isn't useful for my application, would like to use a variable which contains the list of plots.

plot01 = Plot[Sin[x], {x, 0, 6}];
plot02 = Plot[Cos[x], {x, 0, 6}];
plotList = {plot01, plot02};
plotListHold = Thread@HoldForm[{plot01, plot02}]
Export[FileNameJoin[{"C:", "Temp", ToString[#] ~~ ".jpg"}], 
   ReleaseHold[#]] & /@ plotListHold

Some version of the following is what I would like to do. Note that the code attempts to use Map to Apply HoldForm to each item in the variable plotList. The following doesn't work. Have tried various alternatives, but without success.

plot01 = Plot[Sin[x], {x, 0, 6}];
plot02 = Plot[Cos[x], {x, 0, 6}];
plotList = {plot01, plot02};
plotListHold = Map[HoldForm, plotList];
Export[FileNameJoin[{"C:", "Temp", ToString[#] ~~ ".jpg"}], 
   ReleaseHold[#]] & /@ plotListHold

Some discussion on Stack Exchange Suggestions, perhaps for refactoring the approach, and any discussion of the background about what is gong on would be appreciated.

The overall goal is to define a list of all that plots that have been created in a session, then use the Symbol Names for the plots to create strings to be used by when Exporting the plots.

Attachments:
POSTED BY: Robert McHugh
3 Replies

Does this help you?

Am I misunderstanding anything?

Regards

POSTED BY: Neil Singer
Posted 4 years ago

The suggestion you provided was interesting, but is more complicated than I would like. In part, because I would like the notebook to be used and edited by staff with only limited experience with Mathematica. Expect to be exporting lists of up to 100 plots. Want to develop some examples, so that staff can focus on creating plots, feel that they understand what the code is doing, and keep the mechanics of doing that as straight-forward as possible.

For the moment, we are using something like the following, where the HoldForm statement is earlier than in my previous inquiry.

plot01 = Plot[Sin[x], {x, 0, 6}];
plot02 = Plot[Cos[x], {x, 0, 6}];
plotListHold = Thread@HoldForm[ {plot01, plot02}];
Export[FileNameJoin[{"C:", "Temp", ToString[#] ~~ ".jpg"}], 
   ReleaseHold[#]] & /@ plotListHold

Would like to hear more about your warnings about possible pitfalls when using HoldForrm. Apologies for the delayed response.

Attachments:
POSTED BY: Robert McHugh

Robert,

I would take a different approach. HoldForm is difficult to control and it can have strange side effects. I would use notebook programming to grab the input cell and manipulate it. I think it will be more reliable:

First, define a function that will take an input string and parse it, and save the file using the filename. For example, "p1 = Plot[Cos[x], {x, 0, 6}]" gets saved by making a jpg from p1 and calling the file "Plot[Cos[x], {x, 0, 6}].jpg"

saveFile[x_] := 
 Module[{lst}, lst = StringSplit[x, "="]; 
  Export[lst[[2]] <> ".jpg", ToExpression[lst[[1]]], "jpg"]]

Next, make a function to grab the previous input line and send it to saveFile[]: (Note that most of the code below is so that the result is inserted after the existing cell. It makes for a cleaner notebook. If you do not care, many of the lines in this function go away and the save is inserted before the plot)

saveIt2[] := 
 Module[{nb = InputNotebook[], currentCell}, 
  SelectionMove[nb, Previous, CellContents, 3]; (*  go back to the previous input cell *); 
  currentCell = NotebookRead[nb];  (* save the cell *);
  SelectionMove[nb, Next, Cell, 2];   (*  move back down selecting my cell *); 
  SelectionMove[nb, After, Cell];   (* move after my cell *); 
  NotebookWrite[nb, currentCell];  (*   insert the contents of the saved cell *); 
  SelectionMove[nb, All, Cell];   (* select the inserted the cell *); 
  NotebookApply[nb, 
   "saveFile[ToString[\"\[SelectionPlaceholder]\"]]"];   (*  insert the save function *);
  SelectionEvaluateCreateCell[nb] (*  evaluate the new cell and save the file *) ]

To use the code:

p1 = Plot[Cos[x], {x, 0, 6}]

enter image description here

saveIt2[]

This will go and create a new cell with the save function and export the plot. I suppose an upgrade would be to create a function or button that deletes all the saveFile cells when complete so you can reevaluate the notebook without side effects.

I hope this helps.

Regards,

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract