Creating additional figures continuing from one data set

I have an array of positions over time I want to plot as a 2d graph/position trace. However I am dealing with at least 30 data sets so want to create figures with a maximum of say 10 per figure. So I'd have results 1-10 11-20 21-30 as separate but comparable figures. I'm struggling to write the code so that I don't end up with multiple figures of 1-10 or 30 figures each with one plot.
Thanks for any help.

4 Comments

Do you mind to share what you have tried? It will be easier to understand what you mean.
Kevin - unfortunately as it's a work thing I don't know how management would react, but have essentially tried:
plts = totaldata/10
for i = 1:plts
figure
hold on
plot(data,t)
hold off
end
But no luck as I am having a brain block on moving the reference through the data matrix.
Hope that makes things clearer

Sign in to comment.

 Accepted Answer

for filenumber = 1 : 30
filename = ....
load data from filename here
fignum = ceil(filenumber/10)
spnum = 1 + mod(filenumber - 1, 10);
fig = figure(fignum);
ax = subplot(2, 5, spnum, 'Parent', fig) ;
plot(ax, x, y)
title(ax, filename)
end

7 Comments

If I literally wanted separate figs rather than sub plots, can I combine the ax and fig lines from above?
I guess you may change fig = figure(fignum); to
figure;
skip the next line
ax = subplot(2, 5, spnum, 'Parent', fig)
then
plot(x, y)
Then you will have 30 figures at the end of program.
Isn't that the opposite of what you were asking for? You wanted to avoid having 30 figures.
It would be possible to plot multiple files per axes, either as subplot of one figure or as separate figures for each multiple-line plot.
To clarify and reiterate from initial post: I want 3 graphs with 10 plots on each. I don't really like sub plots in the same figure window as the work machines don't have great screens. Hence the issue as like you've said I want to avoid 30 figures, and I don't want 1 figure screen with smaller axes for each of the 3 sub plots.
I feel like I'm missing an obvious solution for this. Either that or I'm asking for an awkward thing...
numfile = 30;
for filenumber = 1 : numfile
filename = ....
load data from filename here
[~, basename, ~] = fileparts(filename);
fignum = ceil(filenumber/10)
spnum = 1 + mod(filenumber - 1, 10);
fig = figure(fignum);
if spnum == 1
ax = axes('parent', fignum);
plot(ax, x, y, 'DisplayName', basename);
hold(ax, 'on');
else
plot(ax, x, y, 'DisplayName', basename);
end
if spnum == 10 || filenumber == numfile
hold(ax, 'off')
legend('show');
drawnow();
end
end
In the above code, the colors and styles for the lines will be chosen automatically. In particular, by default there are only 7 different line colors, so #8, #9 and #10 of each figure will use the same colors as #1 and #2 and #3 respectively. By default for most setups, they will use the same line style (solid) as well. You can adjust that by changing
ax = axes('parent', fignum);
to
ax = axes('parent', fignum, 'LineStyleOrder', '-|--');
in which case although #8, #9, #10 will have the same color as #1, #2, #3, the line will change to dashed instead of solid.
A legend will be created labeling each line as the basic filename without directory or file extension.
The above code does not assume that the number of datasets is an exact multiple of 10.
Walter - Thank you, could you put this code as your answer so I can mark it correct? Very helpful :)
That comment is under my answer, so you can just Accept the answer. It is common (pretty much expected) that the final solution will require some fine tuning that will appear in comments.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!