Multiple figures in for loop

1 view (last 30 days)
Krithika A
Krithika A on 17 Jul 2018
Commented: Star Strider on 18 Jul 2018
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
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).
Krithika A
Krithika A on 18 Jul 2018
Geoff, thank you so much! You helped a lot!
Aquatris, I would like subplots but I was first sorting out the main plots first.

Sign in to comment.

Accepted Answer

Star Strider
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
Krithika A
Krithika A on 18 Jul 2018
Edited: Krithika A on 18 Jul 2018
Thank you so much! Your code plus Geoff's comment helped! To anyone reading, this is what my code ended up like (Edit: To note, I put all my data into one matrix for this):
N = length(y(:,1)); % Getting length of my signal
T = (0:N-1)/fs; % Getting the time axis
yname = {'SS1','SS2','SS3','SS4','SS5','SS6'}; %Subplot titles
ytitle = {'V1','V2','V3','V4'}; % Main plot title
for i2=1:4 % Plotting multiple plots with subplots
figure
for i = 1:6
This_i = (i+((i2-1)*6)); % I have 24 columns in y, where I would like to plot six of them at a time. Thus, 4 plots with 6 subplots each
subplot(2,3,i);
plot(Tt,y(:,This_i));
title([ytitle{i2},' - ',yname{i}]);
end
end
Star Strider
Star Strider on 18 Jul 2018
My (our) pleasure!

Sign in to comment.

More Answers (0)

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!