Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to execute several commands in one text file and use it in notebook by Get[] command. for example:

a=Plot[x^2 ,{x,0,2}];
b=ListPlot[{{0.327081,9.94393},{0.327081,0.102804}},Joined->True];
Show[a,b,PlotRange->All,,PlotLabel->hy]
a1=Plot[26x^2 - 19x,{x,0,3}];
b1=Plot[-2x^2 ,{x,2,3}];
Show[a1,b1,PlotRange->All,PlotLabel->sx]  

I want to see both plots.But Mathematica run only last command in text file

share|improve this question
    
Why can't you run these commands from the notebook? –  s0rce May 2 '13 at 2:53
    
Because my code is too long.i want to short it –  david May 2 '13 at 3:05
add comment

2 Answers

Well, I know of a simple solution: Combine both graphics into a list of graphics.

a=Plot[x^2 ,{x,0,2}];
b=ListPlot[{{0.327081,9.94393},{0.327081,0.102804}},Joined->True];
a1=Plot[26x^2 - 19x,{x,0,3}];
b1=Plot[-2x^2 ,{x,2,3}];
{Show[a,b,PlotRange->All,PlotLabel->hy],Show[a1,b1,PlotRange->All,PlotLabel->sx]}

Kind of simple, I know, but it works. Use //Column or //Row or //TableForm if you need more structure for your graphs. I personally enjoy TableForm the most. As in:

{Show[a,b,PlotRange->All,PlotLabel->hy],Show[a1,b1,PlotRange->All,PlotLabel->sx]}//TableForm

There's another way also! By using Print[]:

a=Plot[x^2 ,{x,0,2}];
b=ListPlot[{{0.327081,9.94393},{0.327081,0.102804}},Joined->True];
Print[Show[a,b,PlotLabel->hy]]
a1=Plot[26x^2 - 19x,{x,0,3}];
b1=Plot[-2x^2 ,{x,2,3}];
Print[Show[a1,b1,PlotRange->All,PlotLabel->sx]]

Don't forget to remove the PlotRange->All on your first Show, it leads to errors because listplot and plot are different. You may use ListLinePlot also instead of Joined->True in listplot.

share|improve this answer
add comment

Your assertion is not correct (that Mathematica only runs the final line). Put your code in a text file (I called it "text.txt"). Then

<<"test.txt"

and it will run the code in the text file. It will not display the plots. You can do that from your notebook. For instance typing

a

shows the plot you have defined as a. You need to execute the Show commands in a notebook, though all the calculations are done when you Get the "test.txt" file. Of course we have already answered this same question for you here yesterday How can I decrease number of lines of code in a notebook?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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