Group Abstract Group Abstract

Message Boards Message Boards

0
|
364 Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Multiple expressions in a notebook Do loop

Posted 4 days ago

I'm doing a Do loop in my Mathematica notebook as I want to inspect plots associated with a list of files under dialog control. So near as I can tell the only way to have multiple expressions in a Mathematica looping structure is to separate them with semicolons. Unfortunately, putting a semicolon after a plot expression will not display the plot in the notebook. So, I assign all the plots to variables and terminate those lines in semicolons and then use the Column statement to present them all at once in a column. So, the Column statement has no semicolon, and although this whole limitation seems beyond stupid to me, at least I'll get my graphics presented with each loop iteration. However, I also want to present a dialog at the end of each loop iteration to halt the looping, inspect the plots and decide whether to make note of this case for more thorough analysis later. Well, that's another statement without a semicolon, else the dialog won't appear. Again, this single expression in a looping structure limitation seems beyond stupid to me. So, does anyone have any ideas how I do more than one non-semicolon expression in a Mathematica looping structure?

POSTED BY: Roger B
6 Replies

No semicolons...?:

note = {}
Do[
 DialogInput[
  DialogNotebook[{
    ExpressionCell[Plot[Sin[n x], {x, 0, 2 Pi}], "Output"],
    TextCell["Wavy enough? "],
    Button["Save", DialogReturn[note = {note, n}]],
    DefaultButton["Skip", DialogReturn[]]
    }]
  ],
 {n, 10}]
Flatten@note
POSTED BY: Michael Rogers

To display a plot from a Do loop, simply wrap it in Print:

Do[Print[Plot[x^n, {x, -1, 1}]], {n, 4}]
POSTED BY: Gianluca Gorni

Do[], Scan[] are iterative structures that return no output (well, actually, each returns Null).

Table[], Map[] are iterative structures that return the values computed in each iteration. Perhaps one of them is what you're looking for.

POSTED BY: Michael Rogers
Posted 4 days ago

This may require a bit of back and forth, because I'm not sure about every detail of your question. Let's start with your final sentence:

does anyone have any ideas how I do more than one non-semicolon expression in a Mathematica looping structure?

You can use any structure you like. It sounds like you already used Column in some fashion. You could also just use List. For what it's worth, you never have more than one expression in the body of a Do loop. Those semicolons aren't really allowing you to add extra expressions. The semicolon is syntactic sugar for CompoundExpression. If you already knew that, then sorry for being pedantic. If you aren't aware of that, then that could be a whole other discussion we can have if you want to pursue it.

Now let's tackle this:

I also want to present a dialog at the end of each loop iteration to halt the looping, inspect the plots and decide whether to make note of this case for more thorough analysis later.

To this I can provide something concrete. I'm sure it's wrong in the details. In particular, I'm avoiding the question of how you're reading from files, but maybe this will get us started toward a solution. Let's say I have these plots:

plots = {Plot[Sin[x], {x, -3, 3}], Plot[Cos[x], {x, -3, 3}], Plot[Sinh[x], {x, -3, 3}]}

enter image description here

I could create a data structure for capturing the notes about each plot:

notes = <||>;

Then I could do the loop:

Do[
 AppendTo[notes, plot -> InputString[plot]],
 {plot, plots}]

The InputString will capture input from the user. After running that loop, we can look at notes. When I did that just now, here's what notes looks like:

notes

enter image description here

Maybe you want some different structure. Maybe you want the keys to be indices instead of plots. Maybe you have filenames you want to use for indices. I'm just demonstrating the idea here, but feel free to ask how to modify this to fit your needs.

Now at this point, I confess I'm confused. You said:

the Column statement has no semicolon, ... at least I'll get my graphics presented with each loop iteration.

That's not what I would expect. You said you were using Do, and Do doesn't produce output at all. Were you maybe also using Print? Anyway, if this doesn't answer your question, maybe you can provide more concrete details or a more prescriptive description of what outputs/behaviors you want.

POSTED BY: Eric Rimbey
Posted 4 days ago

No code. Not worth the effort. English seems to work for most people. You don't understand "looping structure", "single expression", "semi colon", ... ?

POSTED BY: Roger B

Welcome to Wolfram Community!
Please provide your efforts in the form of the Wolfram Language code. This will make it easier for other members to help you. Check several methods available to include your code in the rules http://wolfr.am/READ-1ST

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