Create multiple subplot from one loop

Hi all,
I have 359 data set which can be used to create individual graphs, but that's too much. So i want to create 18 subplots where each have 20 graphs. The code i have, creates a subplot with the 1st 20 graphs and stops. I need it to carry on creating more subplots every 20 graphs until theres no more.
for j=2:length(xt); % xt 359x1 double
subplot(5,4,j-1)
plot(xs,yinternew(:,j)) % xs 17x1 double yinternew 17x359 double
xlabel('Strain, %')
ylabel('H3H0 Torque')
end
Do you guys know how to do this?
Thank you for your help.

 採用された回答

Voss
Voss 2023 年 6 月 16 日

1 投票

% some made-up data, for demonstration:
xs = 1:17;
yinternew = reshape(1:17*359,17,[]);
n_lines = 20; % 20 lines per subplot
n_total = size(yinternew,2); % n_total lines total
for j = 1:ceil(n_total/n_lines)
subplot(5,4,j)
idx = (j-1)*n_lines+2:j*n_lines+1;
idx(idx > n_total) = [];
plot(xs,yinternew(:,idx)) % xs 17x1 double yinternew 17x359 double
xlabel('Strain, %')
ylabel('H3H0 Torque')
end

6 件のコメント

Adam Danz
Adam Danz 2023 年 6 月 16 日
Consider the TiledLayout version instead of using subplot
% some made-up data, for demonstration:
xs = 1:17;
yinternew = reshape(1:17*359,17,[]);
n_lines = 20; % 20 lines per subplot
n_total = size(yinternew,2); % n_total lines total
tcl = tiledlayout(5,4,'TileSpacing','compact');
for j = 1:ceil(n_total/n_lines)
nexttile()
idx = (j-1)*n_lines+2:j*n_lines+1;
idx(idx > n_total) = [];
plot(xs,yinternew(:,idx)) % xs 17x1 double yinternew 17x359 double
end
xlabel(tcl,'Strain, %')
ylabel(tcl,'H3H0 Torque')
S C
S C 2023 年 6 月 18 日
Thank you for your answer.
But i think i may have used the wrong terms to explain what i wanted. So what i want is multiple figures, each figure should have 20 subplots (each plot have only 1 line - 1 data set). The figures should stop until there's no more data set.
So in total there should be 18 figures, each with 20 subplot and each subplot has only 1 line.
I hope this make better sense of what i want.
Thank you :)
Adam Danz
Adam Danz 2023 年 6 月 19 日
These solutions address your question. You need to add a loop that produces the figures.
for i = 1:20
figure()
% <insert subplot or tiledlayout loop
end
Voss
Voss 2023 年 6 月 24 日
% some made-up data, for demonstration:
xs = 1:17;
yinternew = reshape(1:17*359,17,[]);
n_lines = 20; % 20 subplots per figure, each with one line
n_total = size(yinternew,2)-1; % n_total lines total
for k = 1:ceil(n_total/n_lines)
figure()
tcl = tiledlayout(5,4,'TileSpacing','compact');
xlabel(tcl,'Strain, %')
ylabel(tcl,'H3H0 Torque')
for j = 1:n_lines
idx = (k-1)*n_lines+j+1;
if idx > n_total+1
break
end
nexttile(tcl);
% subplot(5,4,j)
plot(xs,yinternew(:,idx)) % xs 17x1 double yinternew 17x359 double
% xlabel('Strain, %')
% ylabel('H3H0 Torque')
end
end
S C
S C 2023 年 6 月 25 日
Hi, thank you. I used all 3 of your suggestions and modified slighlty to make it work.
Dyuman Joshi
Dyuman Joshi 2023 年 6 月 26 日
Hello @S C, if this answer solved your problem, please consider accepting it.
Accepting an answer helps the author to gain reputation points and it also helps others that might face a similar problem in the future.
If there are more than 1 answers, you can also vote for the answers that helped you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2021a

タグ

質問済み:

S C
2023 年 6 月 16 日

コメント済み:

2023 年 6 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by