How to plot multiple figures using for loop

5 views (last 30 days)
DV
DV on 28 Jun 2022
Answered: Devang Tavkari on 6 Jul 2022
Hi.
I have 40 variable data that I want to plot on 3D.
Let's say the data: Data_1, Data_2, Data_3, ...... Data_40.
Each has 43x6.
I first loaded the data from the directory:
LIST=dir('*.mat');
N = length(LIST);
for ii = 1:N
load(['Column_' num2str(ii) '.mat'])
LIST(ii).name(1:end-4);
end
It works when I individually plot the figure:
figure
mapshow((Data_1(:,1:2)),(Data_1(:,3:4)),(Data_1(:,5:6)),'DisplayType','surface');
demcmap((Data_1(:,5:6)));
view(3);
xlabel('x (meter)', 'fontsize',13);
ylabel('y (meter)', 'fontsize',13);
zlabel('z (meter)', 'fontsize',13);
title('Column 1', 'FontSize', 15);
axis normal
The reason why I created mapshow((S(:,1:2)),(S(:,3:4)),(S(:,5:6)),'DisplayType','surface') is because the column 1 and 2 arrays the X-axis, 3 and 4 Y-axis, and 5 and 6 Z-axis.
However, it seems I work a lot with this. So, I want to plot each of Data_1, Data_2,....Data_40 separately using for loop.
So, I write:
for i=1:numel(LIST)
S=(LIST(i).name(1:end-4));
figure(i)
mapshow((S(:,1:2)),(S(:,3:4)),(S(:,5:6)),'DisplayType','surface');
demcmap((S(:,5:6)));
view(3);
end
I don't know how to array the mapshow using for loop.
I am sorry that I am still new using MATLAB.
Appreciate your reply.
Thank you.
  10 Comments
Jan
Jan on 30 Jun 2022
@DV: This does not work in Matlab. Try it:
a = 1:3
a = 1×3
1 2 3
a:(a+1)
ans = 1×2
1 2
The colon operator uses the 1st element only, if an array is provided as input, so a:(a+1) is the same as: a(1):a(1)+1 . Therefore "the plot should be" from you comment above does not clarify, what you want, because it does not work.
I still do not understand, what you want to achieve.
DV
DV on 30 Jun 2022
I realized this too.
As you said, using Data_1, Data_2 and so on is a bad choice, I made it all together instead and modified the code.
Generally speaking, the data of 40 figures is now combined in one data.
As I mentioned, I wanted to create 40 figures using for loop. I thought by using:
a=1:41; will give me automatic count to each column of X axis I want to plot.
(Figure 1 from column 1 and 2, Figure 2 from column 2 and 3, and so on).
But in fact, it was just using the first and second column, which created the first figure only. Well, it was 40 figures at the end, but it plotted all the same (figure 1).
Appreciate your time to help me. Thank you!

Sign in to comment.

Answers (1)

Devang Tavkari
Devang Tavkari on 6 Jul 2022
You need to store the (data1 - data 40) in a cell and then plot the data from the cell using a for loop. so when you load the data from your directrory save it in a cell, later make a for loop of the lengh of that cell and plot the data from every cell. you can later also use saveas command to save your plots.

Categories

Find more on 2-D and 3-D 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!