Hi guys, I have bunch of excel files that almost everything in them is similar. All I want is creating a loop instead of coding 10 figures, that some data is loaded from row (I can get this part done), then using a tiled layout or subplot to click run and MATLAB loops and adds figures to the next tile but I cant get this going:
for r = 1:10
X=xlsread(B) - for instance
tiledlayout(2,5)
scatter(X(the plot works))
nexttile
end

 Akzeptierte Antwort

ANKUR KUMAR
ANKUR KUMAR am 12 Mär. 2021
Bearbeitet: ANKUR KUMAR am 12 Mär. 2021

2 Stimmen

for r = 1:10
subplot(2,5,r)
plot(randi(10,5,5),'ro')
end
If you have two loops, you can use as follows:
for r = 1:2
for k=1:5
subplot(2,5,(r-1)*5+k) % 5 is used because the maximum iteration of k is 5
plot(randi(10,5,5),'ro')
end
end

11 Kommentare

Wolfgang McCormack
Wolfgang McCormack am 12 Mär. 2021
@ANKUR KUMAR thank you Ankur, how can I include the readings?
For instance, instead of plotting randi, I plot
Mysampledata(:,:,r), it gives me an error on the second loop
Wolfgang McCormack
Wolfgang McCormack am 12 Mär. 2021
also, do you know how to do it using tiledlayout?
Mysampledata=randi(20,4,4,10);
for r = 1:10
subplot(2,5,r)
plot(Mysampledata(:,:,r),'ro')
end
Wolfgang McCormack
Wolfgang McCormack am 12 Mär. 2021
@ANKUR KUMAR it gives this error:
Index in position 1 exceeds array bounds (must not exceed 1).
ANKUR KUMAR
ANKUR KUMAR am 12 Mär. 2021
Could you please attach your data (in a mat file), and the code you are using.
@ANKUR KUMAR I copied and pasted yours
This one
Mysampledata=randi(20,4,4,10);
for r = 1:10
subplot(2,5,r)
plot(Mysampledata(:,:,r),'ro')
end
clc
clear
Mysampledata=randi(20,4,4,10);
for r = 1:10
subplot(2,5,r)
plot(Mysampledata(:,:,r),'ro')
end
The above code results into this plot. Make sure to clear all variables in the workspace.
@ANKUR KUMAR thanks it is working now after clearning the data. Still, I cannot get mine going. Here is the code i want:
Aloopsamp1 = [1:1:6; 1:1:6]
Aloopsamp2 = [1:1:6; 1:1:6]
for r = 1:2
subplot(2,5,r)
bar(Aloopsamp(:,:,r))
end
Aloopsamp1 = [1:1:6; 1:1:6]
Aloopsamp2 = [1:1:6; 1:1:6]
Aloopsamp=cat(3,Aloopsamp1,Aloopsamp2)
for r = 1:2
subplot(2,5,r)
bar(Aloopsamp(:,:,r))
end
Or you can use save into cells
clc
clear
Aloopsamp_cell{1} = [1:1:6; 1:1:6]
Aloopsamp_cell{2} = [1:1:6; 1:1:6]
Aloopsamp_cellarray=cat(3,Aloopsamp_cell{:})
for r = 1:2
subplot(2,5,r)
bar(Aloopsamp_cellarray(:,:,r))
end
Wolfgang McCormack
Wolfgang McCormack am 12 Mär. 2021
Thank you so much @ANKUR KUMAR. This is definitely working now. Just a quick question, could you please teach me why we should do it this way?
ANKUR KUMAR
ANKUR KUMAR am 12 Mär. 2021
We need to use cat command to concatenate multiple separate matrices into a single matrix. The first argument in the cat command is the dimension along which you wish to concatenate.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Adam Danz
Adam Danz am 12 Mär. 2021
Bearbeitet: Adam Danz am 12 Mär. 2021

4 Stimmen

> should a nexttile be in the loop?
Yes, but the tiled layout should be defined before the loop.
The first two examples listed in this answer show how to use tiledlayout in a loop with a global legend.
Here's another example.
fig = figure();
tlo = tiledlayout(2,3);
h = gobjects(1,6);
colors = lines(6);
for i = 1:6
ax = nexttile(tlo);
h(i) = plot(ax, 1:10, rand(1,10), 'Color', colors(i,:), 'DisplayName', sprintf('line %d',i));
end
lg = legend(h);
lg.Layout.Tile = 'East'; % <-- place legend east of tiles

1 Kommentar

Wolfgang McCormack
Wolfgang McCormack am 13 Mär. 2021
@Adam Danz thank you so much Adam, you are a savior for me on this forum! I don't know what I had to do without you! thx

Melden Sie sich an, um zu kommentieren.

Wolfgang McCormack
Wolfgang McCormack am 12 Mär. 2021

0 Stimmen

Hey Adam, @Adam Danz
I saw your answer on this post:
Could you please teach me how to corporate that in a for loop? should a nexttile be in the loop?
Thank you!

2 Kommentare

Danish Ali
Danish Ali am 8 Mär. 2022
Hey Adam ! @Adam Danz
I have similar question;
How can we plot multiple figures with subplots using loop?
For example, using loops, MATLAB should generate 17 figures and allocate 10 subplots in each figure.
Figure should have 1 to 10 subplots then figure two should have 11 to 20 and so on.
There are 170 samples. Any idea how it can be done ?
I would appreciate your help.
Adam Danz
Adam Danz am 9 Mär. 2022
Bearbeitet: Adam Danz am 9 Mär. 2022
nFigures = 17; % number of figs
subplotGrid = [5,5]; % subplot grid arrangement
nAxes = prod(subplotGrid);
figHandles = gobjects(1,nFigures);
axesHandles = gobjects(nFigures, nAxes);
for i = 1:nFigures
figHandles(i) = figure();
for j = 1:nAxes
axesHandles(i,j) = subplot(subplotGrid(1),subplotGrid(2),j,figHandles(i));
end
end
Not tested

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by