필터 지우기
필터 지우기

How do I plot a selective range of x-axis values

조회 수: 434 (최근 30일)
Douglas
Douglas 2011년 10월 19일
답변: Kevin Geib 2022년 6월 23일
I am a new user of MATLAB. Currently I have this in my script:
figure
subplot(2,2,1)
plot(t/60,mass)
grid on
title('Mass dried')
xlabel('Time, t (min)')
ylabel('Mass dried, g')
This plots time in minutes on the x-axis, where "t" is the time vector in seconds returned from the function pdepe(m,@solnpde,@solnic,@solnbc,r,t,options). The solution to the pde is from t = 0 to t = 1800.
Please advise how I can plot the graph for values of t from:
1. t = 0 to t = 600
2. t = 600 to t = 1200.
Many thanks,
Doug

채택된 답변

Naz
Naz 2011년 10월 19일
your 't' and 'm' must be the same size; open the variable 't' from Workspace and look what index contains the value of ~600. Let's say the index is 20. Thus, you want to use a part of 'time' array from index 1 to 20. Similarly, you find what index corresponds to value of 1200. If it you found it to be equal let's say 40, then you need to use t[21:40]
subplot(2,1,1)
plot(t(1,1:20),m(1,1:20));
subplot(2,1,2)
plot(t(1,21:40),m(1,21:40));
Of course, there is a way to find corresponding indecies automatically, but if you are planning to use it once, it is easier just to look up for the each index yourselves.
I think here is the way to find'em automatically:
t_temp=t;
t_temp=t_temp-600;
t_temp=abs(t_temp);
index600=find(t_temp==(min(t_temp)),1,'first');
same for the other:
t_temp=t;
t_temp=t_temp-1200;
t_temp=abs(t_temp);
index1200=find(t_temp==(min(t_temp)),1,'first');
subplot(2,1,1)
plot(t(1:index600),m(1:index600));
subplot(2,1,2)
plot(t(index600:index1200),m(index600:index1200));
Now, I hope, this answer is deserved to be accepted.
  댓글 수: 3
Naz
Naz 2011년 10월 19일
t(1,21:40) is 1st row, colums 21:40. This is more general implementation. Since your t and m are only arays (one row), you probably can ignore the '1'. That is:
plot(t(21:40),m(21:40));
Douglas
Douglas 2011년 10월 19일
Thanks

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

추가 답변 (3개)

Kevin Geib
Kevin Geib 2022년 6월 23일
Hey,
another Option if you dont want to change the Plot command itself and see a specific x/y Plot is to limit your axis.
plot(t/60,mass)
grid on
title('Mass dried')
xlabel('Time, t (min)')
ylabel('Mass dried, g')
xlim([0 60])
same can be done with y-axis
ylim([0 100])

Fangjun Jiang
Fangjun Jiang 2011년 10월 19일
t=0:1800;
mass=t;
index=1:600;
figure(1);plot(t(index)/60,mass(index));
index=601:1201;
figure(2);plot(t(index)/60,mass(index));

Tahmid Rahman
Tahmid Rahman 2018년 4월 23일
x=1:0.1:100
if x<20
y1=2*sin(x);
elseif 20<=x , x<40;
y2=cos(3*x);
elseif 40<=x , x<60;
y3=sin(0.3*x);
elseif x<=60
y4=8*sin(x);
elseif y<6
y5= 6;
need to draw a graph for each y condition.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by