2
$\begingroup$

I want to bundle some actions of a task in a module to call it a few times. The task shall display some Graphics and it shall return some data (as a list).

But I can't do that (it would have been possible in Mma 4, where a Semicolon after Show did not suppress the display as in later versions, especially in the current one).

Consider

    Task:= 
Module[{}
      ,co=Table[{x,Cos[x]},{x,0,2*Pi,2*Pi/10}];
       si=Table[{x,Sin[x]},{x,0,2*Pi,2*Pi/10}];

       Show[{ListPlot[co,PlotStyle->Red]
            ,ListPlot[si,PlotStyle->Green]
            ,Plot[Cos[x], {x,0,2*Pi}, PlotStyle->Red]
            ,Plot[Sin[x], {x,0,2*Pi}, PlotStyle->Green]
            }
           ]    (* HERE! no Output, if there is a ; after the bracket *)
       {1,2,co}
      ];                (* Task *)

If I call Task, I get:

strange result

This is a rather strange result! Why are there multiple copies of Show's output interleaved with the list of the line below (the data, I want to return from my Task)? I just expect one image and one list?

If I add a semicolon after the cosing bracket in the line marked "HERE!", I get

expected picture, but not what I am after

which is what I expect, but not what I am after. I want to produce 1st) a graphics (composed of several ones) on the screen, 2nd) a list of results computed in the module.

I know how to produce such an oputput, but to do so I would have to "unbundle" my module.

What is the silver bullet to create both a graphics on display and a result from a Module? (possibly one with parameters unlike my very much simpliefied example).

[3]: https://i.stack.imgur.com/MlsUg.png

$\endgroup$
1
  • $\begingroup$ Just Print your plots, add a semicolon after the Print command, and do the other stuff $\endgroup$ Mar 21, 2016 at 15:40

2 Answers 2

2
$\begingroup$

Why are there multiple copies of Show's output interleaved with the list of the line below (the data, I want to return from my Task)?

You are performing list multiplication on graphics and data. Effectively {a}*{b,c,d}.

What is the silver bullet to create both a graphics on display and a result from a Module?

Return a list

Task := Module[{}, co = Table[{x, Cos[x]}, {x, 0, 2*Pi, 2*Pi/10}];
   si = Table[{x, Sin[x]}, {x, 0, 2*Pi, 2*Pi/10}];
   {Show[{ListPlot[co, PlotStyle -> Red], 
      ListPlot[si, PlotStyle -> Green], 
      Plot[Cos[x], {x, 0, 2*Pi}, PlotStyle -> Red], 
      Plot[Sin[x], {x, 0, 2*Pi}, 
       PlotStyle -> Green]}]    ,(*HERE! no Output,if there is a;
    after the bracket*) {1, 2, co}}];

Alternatively, if you have v10, us an Association

Task:= Module[{a = Association[]},
  a["plot"]=Plot[Cos[x],{x,0,2 Pi}];
  a["data"]={1,2,3};
  a
]

Now your plot can be extracted with Task["plot"] and your data with Task["data"]

$\endgroup$
1
  • $\begingroup$ Thank you for the explanation. I did not know about Association before. But I looked it up immediately. That one looks good to me because it is expandable to return several different categories of results such bthat one can easily extract the right one from the result without so much danger to miscount the position in the resultif it is a list as in your first example. $\endgroup$ Mar 22, 2016 at 17:28
1
$\begingroup$

Maybe not very elegant but you could just have Task return all of the desired items and then display them outside of Task:

Task := Module[{si, co, x, g}, 
  co = Table[{x, Cos[x]}, {x, 0, 2*Pi, 2*Pi/10}]; 
  si = Table[{x, Sin[x]}, {x, 0, 2*Pi, 2*Pi/10}]; 
  g = Show[{ListPlot[co, PlotStyle -> Red], 
     ListPlot[si, PlotStyle -> Green], 
     Plot[Cos[x], {x, 0, 2*Pi}, PlotStyle -> Red], 
     Plot[Sin[x], {x, 0, 2*Pi}, PlotStyle -> Green]}];
   {g, 1, 2, co}]
results = Task;
results[[1]]
results[[{2, 3, 4}]]
$\endgroup$

Your Answer

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

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