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 个评论

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);
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
Hey. Thanks for answering
No, just a "clc"
Im in doubt how i should name the the variables time, vol, plot etc to get 7 figures.
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
It dosent seems to help. I have made this graph for the first file in another script. i was hoping one could make a script so i could get 7 similar subplots
.
____
Now i have to re-do it 7 times but with-in a loop.
Sorry if i am completly retarded. I am new to Matlab.
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
Hey Mathieu. Was in Paris a couple of days therefore im a litle late with the answer.
The files is in a zip folder.
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 个)

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
Ditmer Kiis 2022-4-13
Hey
I forgot to answer you.
Thank you very much for your time and patience! I am extremely grateful
Paris was great, superb weather (guess we were lucky), good food, vine etc..
Greetings!

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by