Plotting different figures by means of for loop
3 views (last 30 days)
Show older comments
Riccardo Zabatta
on 3 Aug 2021
Commented: the cyclist
on 3 Aug 2021
Hi!
I have a problem with plotting by means of a for loop.
I'll try to explain my case.
I have a 3x4 cell containing mx2 arrays (the two columns represent displacement and load of FE analyses I am performing).
I want to plot load-displacement curves; I want 1 graph per cell row, on each graph 4 curves will be displayed (1 per cell column).
I'll show you how I have tried so far:
for i = 1:cellrows
figure(i);
if j < cellcolumns+1
plot(cell{i,j}(:,1),cell{i,j}(:,2))
hold on
else
hold off
j = 1;
end
end
This is producing the 3 graphs I want, but only 1 curve per graph is displayed (I guess the fourth one, haven't checked exactly). What am I missing?
Hope it's clear, thanks for anyone who's answering.
0 Comments
Accepted Answer
the cyclist
on 3 Aug 2021
I believe this does what you are trying to do:
% Create some fake data [Use your real data instead]
m = 5;
C = cell(3,4);
C = cellfun(@(x)[(1:m)' rand(1)*(1:m)'],C,'UniformOutput',false);
% Get the size of the cell array
[cellrows,cellcolumns] = size(C);
for i = 1:cellrows % Loop over the rows
figure(i); % New figure for each row
hold on % Make sure each plot does not overwrite the others in this row
for j = 1:cellcolumns
plot(C{i,j}(:,1),C{i,j}(:,2))
end
end
1 Comment
the cyclist
on 3 Aug 2021
Also, it is strongly recommended to not use cell as a variable name, because it is a MATLAB function.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

