make a loop for plotting diagrams

조회 수: 1 (최근 30일)
Skydriver
Skydriver 2018년 10월 18일
댓글: Kevin Chng 2018년 10월 18일
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일
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
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by