Multiple figures in for loop
1 view (last 30 days)
Show older comments
Hello,
I would like to run a for loop that produces multiple plots. I looked at other questions that ask this, but they don't seem to fit the data set I have.
This is code I have, if written out (without for loop):
N = length(y1(:,1));
T = (0:N-1)/fs;
figure
subplot(2,3,1)
plot(T,y1(:,1))
subplot(2,3,2)
plot(T,y2(:,1))
subplot(2,3,3)
plot(T,y3(:,1))
subplot(2,3,4)
plot(T,y4(:,1))
subplot(2,3,5)
plot(T,y5(:,1))
subplot(2,3,6)
plot(T,y6(:,1))
This is the code I have so far with the for loop:
figure
for q = 1:length(txtfiles); % This is the number of files I have for my data (in numerical matrices), I have 24 files that I'm interested in, where I'd like to look at 6 files at a time and only the first column
cnt = 1;
for bor=1:6:24
plot(T,y1(:,1))
cnt=cnt+1;
end
end
But I get stuck because I can't do this:
figure
for q = 1:length(txtfiles);
cnt = 1;
for bor=1:6:24
plot(T,y(q)(:,1))
plot(T,y(q+1)(:,1))
plot(T,y(q+2)(:,1))
plot(T,y(q+3)(:,1))
plot(T,y(q+4)(:,1))
plot(T,y(q+5)(:,1))
cnt=cnt+1;
end
end
Thank you for the help in advance!
3 Comments
Geoff Hayes
on 17 Jul 2018
Krithika - in your original code, you seem to have six variables for each of the subplots. If instead of using individual variables but have a cell array or matrix (if the dimensions for each y* are the same) then you can easily iterate over your single array like
for k=1:6
subplot(2,3,k);
plot(T,allData(:,1,k));
end
where allData is your three-dimensional matrix that has all data from all variables (assuming that dimensions for all y* are the same).
Accepted Answer
Star Strider
on 17 Jul 2018
I would first concatenate all the (unfortunately numbered) ‘y#’ variables into a single matrix:
y = cat(3, y1,y2,y3,y4,y5,y6);
then address them such that the third dimension is now the previous number, so for example:
plot(T,y(:,1,q))
iterating ‘q’ so that ‘y3(:,1)’ is now ‘y(:,1,3)’ and so for the others.
2 Comments
More Answers (0)
See Also
Categories
Find more on Line Plots 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!