How do I plot a selective range of x-axis values
391 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Naz
on 19 Oct 2011
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 Comments
Naz
on 19 Oct 2011
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));
More Answers (3)
Kevin Geib
on 23 Jun 2022
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])
0 Comments
Fangjun Jiang
on 19 Oct 2011
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));
0 Comments
Tahmid Rahman
on 23 Apr 2018
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!