For loop with subplots with uploaded
Show older comments
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 Comments
Mathieu NOE
on 24 Mar 2022
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
on 24 Mar 2022
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
on 24 Mar 2022
Ditmer Kiis
on 24 Mar 2022
Mathieu NOE
on 24 Mar 2022
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
on 24 Mar 2022
Mathieu NOE
on 25 Mar 2022
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
on 29 Mar 2022
Mathieu NOE
on 29 Mar 2022
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
Answers (2)
Voss
on 24 Mar 2022
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
on 13 Apr 2022
0 votes
1 Comment
Mathieu NOE
on 13 Apr 2022
hello Ditmer
good news !
all the best
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
