how to plot values that only fall within a range?

have an excel file where i am plotting the values that are around 113 KPH. there are thousands of rows for that specifc column and i just wanna plot the Y values that fall between 111KPH and 114 KPH. I also am trying to add the average of those data points but that doesnt seem to work as well. I attached the plot which is incorrect and for some reason is between 0 and 1.
code is:
%%% 70 MPH KPH vs Time
filename = 'TeslaData_Coastdown Analysis_7_26_2019.xlsx';
figure(1)
X = xlsread(filename,'70 MPH','A9:A2000');%adjust according to column length
Y = xlsread(filename,'70 MPH','B9:B2000');%adjust according to column length
FVY = (Y >= 111 & Y <= 114)
KPH_70 = mean(FVY);
yyaxis left
plot(X,FVY);
yyaxis right
plot(X,KPH_70);

 Accepted Answer

Matt J
Matt J on 15 Jan 2020
Edited: Matt J on 15 Jan 2020
KPH_70 = mean(Y(FVY));
yyaxis left
plot(X(FVY),Y(FVY));
yyaxis right
plot(X(FVY),KPH_70*ones(size(FVY)));

17 Comments

didn't work, i uploaded the plot and still shows values that are below the 111
never mind Matt it does filter the values that lie outside the range. only issue is the average curve.
only issue is the average curve.
What is the issue?
as shown in the plot, the average curve doesn't aling with the filtered data.
i edited the code you submitted as well.
KPH_70 = mean(Y(FVY));
yyaxis left
plot(X(FVY),Y(FVY));
yyaxis right
plot(X(FVY),KPH_70);
what exactly does this *ones(size(FVY))); mean?
Matt J
Matt J on 15 Jan 2020
Edited: Matt J on 15 Jan 2020
I assumed you want a horizontal line marking the mean across Y(FVY) and that's what your attached plot seems to show. If that's not what you want, what should it be?
that's exactly what I want but would there be a way to make both axis line up? Thanks for the help, really appreciate it!
You mean so that the two y-axes span the same range? Here's one way,
KPH_70 = mean(Y(FVY));
yyaxis left
plot(X(FVY),Y(FVY));
yl=ylim;
yyaxis right
plot(X(FVY),KPH_70*ones(1,nnz(FVY)),'-' )
ylim(yl);
didn't work, i'll firgure it out someway. Thanks so much for the help Matt!
I think I fixed it. However, I don't really understand why you have two y-axes as opposed to putting multiple plot lines on a single y-axis.
how would i be able to do that? kind new to matlab
Just by doing
plot(X(FVY),Y(FVY), X(FVY),KPH_70*ones(size(FVY)) );
isamh
isamh on 15 Jan 2020
Edited: isamh on 15 Jan 2020
i tried this earlier but only one curve shows. not sure why, when it comes to the KPH_70 , nothing happens.
also, what does *ones(size(FVY)) mean?
I'm really sorry for asking so many questions!
also, what does *ones(size(FVY)) mean?
It creates a vector of ones the same size as FVY, e.g.,
>> FVY=rand(1,5)
FVY =
0.8184 0.3599 0.3480 0.2924 0.0110
>> ones(size(FVY))
ans =
1 1 1 1 1
i tried this earlier but only one curve shows.
Seems doubtful that you tried exactly what I've shown. If you didn't know what ones(size(FVY)) even means, how would it have occurred to you to try it?
isamh
isamh on 15 Jan 2020
Edited: isamh on 15 Jan 2020
haven't tried that specifically. I tried to plot(X,FVY,X,KPH_70) and get same results as
plot(X(FVY),Y(FVY), X(FVY),KPH_70*ones(size(FVY)) );. what's important is that both don't work. Also, when I try *ones(size(FVY)) command window says that they aren't the same length.
I'll manage with what I have right now, hopefully i'll figure it out tonight.
thanks for everything Matt!
Sorry, it should be
plot(X(FVY),Y(FVY),'x--', X(FVY),KPH_70*ones(1,nnz(FVY)),'-' )
Here is an example,
X=1:10;
Y=rand(size(X));
FVY=X>3;
KPH_70=mean(Y(FVY));
plot(X(FVY),Y(FVY),'x--', X(FVY),KPH_70*ones(1,nnz(FVY)),'-' )
untitled.png
works great, Thanks so much!!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 15 Jan 2020

Commented:

on 15 Jan 2020

Community Treasure Hunt

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

Start Hunting!