How do I update several graphs in one loop?

I'm analyzing the results of a Monte Carlo run. I have 1,000 files of seven variables, each recorded at 2,001 timesteps. The data is stored as double floats. I want to generate six plots (the seventh variable is time) with overlays of each variable vs. time. At the end, I want six plots with 1,000 curves each.
I have a loop that cycles through the 1,000 files and extracts information from each. I'd like to build these graphs in the same loop. But I can only get a plot of the data read from the current file. How do I get six different plots to be "sticky"?

4 Comments

Never mind. After trying at least a dozen suggestions over days, one of them plus the manual got me to the answer. The salient portion of my solution looks like this:
tiledlayout(2,1);
ax1 = nexttile;
ax2 = nexttile;
for ifile=1:1000
hold(ax1,'on');
plot(ax1, DataIn(:,1),DataIn(:,2));
hold(ax1,'off');
hold(ax2,'on');
plot(ax2, DataIn(:,1),DataIn(:,3));
hold(ax2,'off');
end
I decided I like the tiled output look for these plots. If I can do it for two, I can do it for six! I hope this helps anyone else faced with a similar problem.
This would be FAR more efficient to build an array of 2001x1000x6 first, then use the vectorized nature of plot() to plot each plane in turn for the six plots.
Or don't bother with the 3D array, just build each plane, plot, and go on to the next in turn.
Since I'm processing and plotting this data in different ways, I'll try building the 3D array first. This is my first MATLAB script,so I built it in a piecewise, ad hoc manner. I'm sure it can be more efficient! Thanks for the tip. I do like writing efficient code.
NB: when you get to plotting that plot() is vectorized to treat columns as separate variables -- and the x variable can be a vector while the y-values are a 2D array. Just the X vector length must match the number of rows in Y.
That's where you'll get the speedup -- there was a Q? just last week where an individual was plotting some 60K points in a loop and it was taking some 10 minutes or more...<60k-lines-on-one-plot-too-slow?> I'd suggest reading it for some other ideas -- even "only" 1000 lines on a single plot probably isn't the best idea.

Sign in to comment.

Answers (0)

Products

Release

R2022a

Asked:

on 28 Jun 2022

Commented:

dpb
on 28 Jun 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!