For loop with subplots with uploaded
이전 댓글 표시
Hallo :)
I am trying to conduct seven subplots with three graphs each. I have conducted one subplots but i have a hard time naming the codes inside the subplote correctly. Can you help me?

댓글 수: 9
Mathieu NOE
2022년 3월 24일
hi Ditmer
inside the for loop you have to create a new figure at each loop iteration
so before the line "subplot(3,1,1)" insert a new line with :
figure(i);
Mathieu NOE
2022년 3월 24일
I suspect that you created one figure object at the very beginning of the code , but we cannot see it because i guess it's hidden by the "Run to Here" windows on top of the editor window
so this line should be removed / commented and remove also the "hold on" which is then no more needed
Ditmer Kiis
2022년 3월 24일
Ditmer Kiis
2022년 3월 24일
Mathieu NOE
2022년 3월 24일
I would suggest that you do the data loading inside the for loop instead of writting seven fil variables at the beginning
also please do not use i or j as loop index as they are normally reserved for complex numbers (i² = -1 , j² = -1)
for k = 1:7
% load data
filename = ['s1_r' num2str(k) '.txt']; % file to be loaded
fil = ReadVerTxt(filename,3);
time = ...
vol = ...
% plot
figure(k)
subplot(311) .... % etc etc
end
Ditmer Kiis
2022년 3월 24일
Mathieu NOE
2022년 3월 25일
hi Ditmer
no one will blame you because you are new to Matlab (everybody was a novice too some day !)
I can further help you if you can share the data files (zip them) + your code (including the subfunctions for reading the text files)
Mathieu
Ditmer Kiis
2022년 3월 29일
Mathieu NOE
2022년 3월 29일
Hi Ditmer
hope you enjoyed your stay in Paris (that's where I work - to be more precise 70 kms south of Paris)
try this code :
clc
clearvars
for k = 1:7
% load data
filename = ['s1_r' num2str(k) '.txt']; % file to be loaded
fil = readmatrix(filename,"NumHeaderLines",7);
time = fil(:,1);
flowrate = fil(:,2);
vol = fil(:,3);
cumsum_flowrate = cumsum(flowrate);
time_step = time(2)-time(1);
vol_kontrol = -time_step * cumsum_flowrate;
figure(k)
subplot(3,1,1);
plot(time,vol,'LineWidth',3);
xlabel('time');
ylabel('vol');
subplot(3,1,2);
plot(time,flowrate,'LineWidth',3);
subplot(3,1,3)
plot(time,vol_kontrol,'LineWidth',3);
end
답변 (2개)
Voss
2022년 3월 24일
files = sprintfc('s1_r%d.txt',1:7).';
for ii = 1:numel(files)
fil = ReadVerTxtData(files{ii},3);
time = fil(:,1);
flowrate = fil(:,2);
vol = fil(:,3);
% sum_flowrate = sum(flowrate);
cumsum_flowrate = cumsum(flowrate);
time_step = time(2)-time(1);
vol_kontrol = -time_step * cumsum_flowrate;
figure();
subplot(3,1,1);
plot(time,vol,'LineWidth',3);
xlabel('time');
ylabel('vol');
subplot(3,1,2);
plot(time,flowrate,'LineWidth',3);
subplot(3,1,3)
plot(time,vol_kontrol,'LineWidth',3);
end
Ditmer Kiis
2022년 4월 13일
0 개 추천
댓글 수: 1
Mathieu NOE
2022년 4월 13일
hello Ditmer
good news !
all the best
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

