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
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

