make a loop for plotting diagrams

1 vue (au cours des 30 derniers jours)
Skydriver
Skydriver le 18 Oct 2018
Commenté : Kevin Chng le 18 Oct 2018
I have 40 data and each of them consist of two colom (period and amplitude). I want to know how to plot those data by using looping. For example my data are XA20120601, XB20120704, XL20110603, XD20140403, XC20130531, XB20090314, XA20110801, XA20080704, XL20170603, XB20140403, XC20130531, XB20100314 etc. Maybe any one can help me to solve the problem.
  2 commentaires
Skydriver
Skydriver le 18 Oct 2018
Modifié(e) : Stephen23 le 18 Oct 2018
This is my coding :
file11='XA20120601.txt';
[d11(:,1), d11(:,2), d11(:,3), d11(:,4)]=textread(file11, '%f%f%f%f', 'headerlines', 5);
file12='XB20120704.txt';
[d12(:,1), d12(:,2), d12(:,3), d12(:,4)]=textread(file12, '%f%f%f%f', 'headerlines', 5);
figure(1);
grid on
plot(d11(:,1),d11(:,2),'r')
hold on
plot(d12(:,1),d12(:,2),'b')
xlabel('Time (s)')
ylabel('Acceleration(g)')
legend('Original Accelerogram','Matched Accelerogram')
Kevin Chng
Kevin Chng le 18 Oct 2018
Refer to my command below the answer. Kindly accept it if it works for you.

Connectez-vous pour commenter.

Réponses (1)

Kevin Chng
Kevin Chng le 18 Oct 2018
C = cat(3, XA20120601, XB20120704, XL20110603, XD20140403, XC20130531 , XB20090314, XA20110801, XA20080704, XL20170603, XB20140403, XB20100314 )
for i:1:1:11
figure()
plot(C(:,1,i),C(:,2,i))
end
Concatenate them into 3 dimension, subsequently use indexing method for your loop.
  5 commentaires
Stephen23
Stephen23 le 18 Oct 2018
Modifié(e) : Stephen23 le 18 Oct 2018
@Akhmad Muktaf: In your question you wrote that "I have 40 data and each of them consist of two colom (period and amplitude)", but now you have shown code that import four columns of data.
So what do you really have, two columns, or four columns, or something else?
It would be much easier if you uploaded some sample files by clicking the paperclip button.
Also you need to explain the filenames, and how these correspond to "north", "south", etc. in the titles.
Kevin Chng
Kevin Chng le 18 Oct 2018
Hi Akhmad, Have you took a look at my answer?
As what I see from your questions and replies
1) 40 dataSets that have 4 columns each.
I guess the 3th and 4th columns are not in use.
2) You load your variable into d1...,d2....,d3.....
Don't name them as d1,d2,d3.... it is very difficult for you to do indexing for your for loop.
Concatenate them into 3 dimension array.
[d(11,:,1), d(11,:,2), d(11,:,3), d11(11,:,4)]=textread(file11, '%f%f%f%f', 'headerlines', 5);
3) each plot has their own unique title.
Put your titles name of all figure into a string array.
titlename = ("Java, Indonesia, CISI 2006-08-29 205916, 5.1","......","......",.....)
I think you want 40 figure plots, therefore, your foor loop should look like this
for i=1:1:40
figure(i);
grid on;
plot(d(i,:,1),d(i,:,2),'r');
xlabel('Time (s)');
ylabel('Acceleration(g)');
legend('Original Accelerogram','Matched Accelerogram');
title(titlename(i));
end

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by