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);

 Respuesta aceptada

Matt J
Matt J el 15 de En. de 2020
Editada: Matt J el 15 de En. de 2020
KPH_70 = mean(Y(FVY));
yyaxis left
plot(X(FVY),Y(FVY));
yyaxis right
plot(X(FVY),KPH_70*ones(size(FVY)));

17 comentarios

isamh
isamh el 15 de En. de 2020
didn't work, i uploaded the plot and still shows values that are below the 111
isamh
isamh el 15 de En. de 2020
never mind Matt it does filter the values that lie outside the range. only issue is the average curve.
Matt J
Matt J el 15 de En. de 2020
only issue is the average curve.
What is the issue?
isamh
isamh el 15 de En. de 2020
Editada: Matt J el 15 de En. de 2020
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 el 15 de En. de 2020
Editada: Matt J el 15 de En. de 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?
isamh
isamh el 15 de En. de 2020
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!
Matt J
Matt J el 15 de En. de 2020
Editada: Matt J el 15 de En. de 2020
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);
isamh
isamh el 15 de En. de 2020
didn't work, i'll firgure it out someway. Thanks so much for the help Matt!
Matt J
Matt J el 15 de En. de 2020
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.
isamh
isamh el 15 de En. de 2020
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 el 15 de En. de 2020
Editada: isamh el 15 de En. de 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!
Matt J
Matt J el 15 de En. de 2020
Editada: Matt J el 15 de En. de 2020
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 el 15 de En. de 2020
Editada: isamh el 15 de En. de 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!
Matt J
Matt J el 15 de En. de 2020
Editada: Matt J el 15 de En. de 2020
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
isamh
isamh el 15 de En. de 2020
works great, Thanks so much!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de En. de 2020

Comentada:

el 15 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by