필터 지우기
필터 지우기

Help with how to exclude zero and show a more efficient way to display the velocity and time in one line?

조회 수: 1 (최근 30일)
I cant figure out how to exclude zero so that when the min velocity and the time the min velocity occurs to not show zero basically I want to show the first velocity and time thats not zero and to display the velocity and time together in a more efficient way??
clear;
clc;
veldata = dlmread('veldata.txt',' ');
time = veldata(:,1);
velocity = veldata(:,2);
if (time > 0)
time = time;
end
[min_velocity,min_ind] = min(velocity);
[max_velocity,max_ind] = max(velocity);
avg_velocity = mean(velocity);
t_minv = time(min_ind);
t_maxv = time(max_ind);
disp('The min velocity at time = ')
disp(min_velocity)
disp(t_minv)
disp(' The max velocity at time in seconds is = ')
disp(max_velocity)
disp(t_maxv)
disp('The average velocity is: ')
disp(avg_velocity)

채택된 답변

Star Strider
Star Strider 2014년 11월 4일
Change the first few lines of your code to these:
veldata = dlmread('veldata.txt',' ');
timev = veldata(:,1); % Changed ‘time’ To ‘timev’
velocityv = veldata(:,2); % Changed ‘velocity’ To ‘velocityv’
% ADD THESE LINES
time = timev(timev>0);
velocity = velocityv(velocityv>0);
The rest of your code is unchanged.
I kept the original data for ‘velocity’ as ‘velocityv’ and ‘time’ as ‘timev’ in the event you want them intact for later processing or plotting.
The definition of ‘time’ and ‘velocity’ from them uses ‘logical indexing’. (It would otherwise require the find function to do the same thing.)
  댓글 수: 2
Jason
Jason 2014년 11월 4일
Thank you so much! By the way do you happen to know how I can display the velocity and time in one statement instead of two right now it looks like this in the command window
The min velocity at time =
12.4000
0.4000
The max velocity at time in seconds is =
22
2.8000
The average velocity is:
18.4000
Star Strider
Star Strider 2014년 11월 4일
Yes!
Use the fprintf function:
fprintf(1,'\nThe min velocity at time = %.2f is %.2f\n', t_minv, min_velocity)
fprintf(1,'\nThe max velocity at time = %.2f is %.2f\n', t_maxv, max_velocity)
fprintf(1,'\nThe average velocity is %.2f\n', avg_velocity)
(The ‘1’ file identifiers denoting standard output — the Command Window — aren’t strictly necessary now, but they once were and I’m in the habit of using them.)

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 11월 4일
You forgot to attach veldata.txt. You can set graphing limits with the xlim() and ylim() functions. Set them up to not include 0 if you don't want 0 on your axes. If that doesn't do it, then attach your data so we can see what you're talking about. http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by