make a loop for plotting diagrams
Show older comments
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 Comments
Kevin Chng
on 18 Oct 2018
Refer to my command below the answer. Kindly accept it if it works for you.
Answers (1)
Kevin Chng
on 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 Comments
Kevin Chng
on 18 Oct 2018
Edited: Kevin Chng
on 18 Oct 2018
Edit for your code. Use 3 dimension array instead of name your variable in order. It facilitate your to use indexing for your for loop.
file11='XA20120601.txt';
[d1(1,:,1), d1(1,:,2), d1(1,:,3), d1(1,:,4)]=textread(file11, '%f%f%f%f', 'headerlines', 5);
file12='XB20120704.txt';
[d1(2,:,1), d1(2,:,2), d1(2,:,3), d1(2,:,4)]=textread(file12, '%f%f%f%f', 'headerlines', 5);
figure(1);
grid on
for i=1:1:2
plot(d1(i,:,1),d1(i,:,2),'r')
hold on
end
xlabel('Time (s)')
ylabel('Acceleration(g)')
legend('Original Accelerogram','Matched Accelerogram')
hold off
Skydriver
on 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
on 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
Categories
Find more on Graphics Performance 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!