make a loop for plotting diagrams

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 件のコメント

Skydriver
Skydriver 2018 年 10 月 18 日
編集済み: Stephen23 2018 年 10 月 18 日
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 2018 年 10 月 18 日
Refer to my command below the answer. Kindly accept it if it works for you.

サインインしてコメントする。

回答 (1 件)

Kevin Chng
Kevin Chng 2018 年 10 月 18 日

0 投票

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 件のコメント

Kevin Chng
Kevin Chng 2018 年 10 月 18 日
編集済み: Kevin Chng 2018 年 10 月 18 日
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
Skydriver 2018 年 10 月 18 日
Actually with my coding I can make a plot, but I will be take more time if I have input for instance 40 data or more. I need a looping process but I don't know how to do.
Skydriver
Skydriver 2018 年 10 月 18 日
編集済み: Stephen23 2018 年 10 月 18 日
These are another format of my plotting code:
clear all;
clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
file11='Matched_TNG20081026CISI_N.txt';
[d11(:,1), d11(:,2), d11(:,3), d11(:,4)]=textread(file11, '%f%f%f%f', 'headerlines', 5);
file12='Original_TNG20081026CISI_N.txt';
[d12(:,1), d12(:,2), d12(:,3), d12(:,4)]=textread(file12, '%f%f%f%f', 'headerlines', 5);
file21='Matched_TNG20060717BOG5_6_E.txt';
[d21(:,1), d21(:,2), d21(:,3), d21(:,4)]=textread(file21, '%f%f%f%f', 'headerlines', 5);
file22='Original_TNG20060717BOG5_6_E.txt';
[d22(:,1), d22(:,2), d22(:,3), d22(:,4)]=textread(file22, '%f%f%f%f', 'headerlines', 5);
file31='Matched_TNG20090507CISI_N.txt';
[d31(:,1), d31(:,2), d31(:,3), d31(:,4)]=textread(file31, '%f%f%f%f', 'headerlines', 5);
file32='Original_TNG20090507CISI_N.txt';
[d32(:,1), d32(:,2) d32(:,3) d32(:,4)]=textread(file32, '%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')
title('Java, Indonesia, CISI 2006-08-29 205916, 5.1')
saveas(gcf,'Java, Indonesia, CISI 2006-08-29 205916, 5.1.jpeg')
figure(2);
grid on
plot(d21(:,1),d21(:,2),'r')
hold on
plot(d22(:,1),d22(:,2),'b')
xlabel('Time (s)');
ylabel('Acceleration(g)')
legend('Original Accelerogram','Matched Accelerogram')
title('South of Java, Indonesia, BOG 2006-07-17 154600, 5.9')
saveas(gcf,'South of Java, Indonesia, BOG 2006-07-17 154600, 5.9.jpeg')
figure(3);
grid on
plot(d31(:,1),d31(:,2),'r')
hold on
plot(d32(:,1),d32(:,2),'b')
xlabel('Time (s)');
ylabel('Acceleration(g)')
legend('Original Accelerogram','Matched Accelerogram')
title('Java, Indonesia, CISI 2009-05-07 070336, 5')
saveas(gcf,'Java, Indonesia, CISI 2009-05-07 070336, 5.jpeg')
Stephen23
Stephen23 2018 年 10 月 18 日
編集済み: Stephen23 2018 年 10 月 18 日
@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 2018 年 10 月 18 日
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

質問済み:

2018 年 10 月 18 日

コメント済み:

2018 年 10 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by