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

 채택된 답변

Matt J
Matt J 2020년 1월 15일
편집: Matt J 2020년 1월 15일

0 개 추천

KPH_70 = mean(Y(FVY));
yyaxis left
plot(X(FVY),Y(FVY));
yyaxis right
plot(X(FVY),KPH_70*ones(size(FVY)));

댓글 수: 17

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

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Objects에 대해 자세히 알아보기

태그

질문:

2020년 1월 15일

댓글:

2020년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by