How to plot multiple figures using for loop

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 个评论

Could you be more specific? Do you want to plot all of them in the same axes?
Hi, thank you for your response.
I want to plot in different figures.
Since I have 40 data, the figure should be 40 too.
What is the purpose of this part of the code:
load(['Column_' num2str(ii) '.mat'])
LIST(ii).name(1:end-4);
The 2nd line does nothing. Maybe you mean (sprintf looks simpler):
LIST(ii).name = load(sprintf('Column_%d.mat', ii));
What is the contents of the field "name"? What is stored in the MAT files?
Thank you for response.
The contents of name is actually the list of data:
Data_1
Data_2
....
Data 40
Each has coordinate information:
Column 1 and Column 2 : X1 and X2
Column 3 and Column 4: Y1 and Y2
Column 5 and Column 6: Z1 and Z2
For example:
667412.021 667420.293 9135496.398 9135496.368 1119.288 1118.148
...
43 rows
Really appreciate your response.
Thank you so much!
Just a note: "Data_1" is a bad choice. Prefer to use arrays instead of hiding indices in the names of variables.
Thank you for the note.
I have tried to make all data in one.
Data= XYZ (43x123)
Column 1:41 for X data
Column 42:82 for Y data
Column 43:123 for Z data
The plot should be:
load ('XYZ')
for ii = 1:N
jj=123;
a=1:41;
b=42:82;
c=83:123;
figure(ii)
mapshow((XYZ(:,a:(a+1))),(XYZ(:,b:(b+1))),(XYZ(:,c:(c+1))),'DisplayType','surface');
demcmap((XYZ(:,c:(c+1))));
view(3);
xlabel('x (meter)', 'fontsize',13);
ylabel('y (meter)', 'fontsize',13);
zlabel('z (meter)', 'fontsize',13);
axis normal
end
It gives me the same figure where supposed to be 40 different figures.
I feel like getting closer to the right code, but still not sure where I need to change.
Thank you.
What does a:(a+1) mean, if a is the vector 1:41 ?
a:(a+1) means the second column for the X-axis.
First figure should be:
a and a:(a+1) in X axis
which means the first figure will have column 1 and column 2 to plot in X-axis
the second figure will be column 2 and column 3
the third figure will be column 3 and column 4
and so on until the last figure.
@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.
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!

请先登录,再进行评论。

回答(1 个)

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.

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

提问:

DV
2022-6-28

Community Treasure Hunt

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

Start Hunting!

Translated by