필터 지우기
필터 지우기

How can I plot multiple results from a function on the same graph?

조회 수: 1 (최근 30일)
Aaron
Aaron 2013년 12월 17일
편집: Image Analyst 2013년 12월 18일
Hello everyone,
I'm working on a Windchill conversion assignment, any guidance would be highly appreciated.
I want to display each result from the function (windchill_calc) - which takes a pre-defined temperature vector and a user inputted vector (of 5) and works out windchill (W_2)
Except the plot is only showing the result from the 5th value of the vector, I understand why this is happening and that this is quite a simple error but I can't work out a solution - any ideas?
(temp_vector is previously defined)
_______________________
windspeed_user = input('please enter a 5 windspeed vector'); %requesting input from user.
i = 1;
for i = 1:1:5;
W_2 = Windchill_calc(temp_vector, windspeed_user(i)) %calls function
end
plot (W_2, temp_vector)
xlabel ('Temperature (Fahrenheit)')
ylabel ('Windchill results')
grid on

답변 (2개)

David Sanchez
David Sanchez 2013년 12월 17일
I guess you have to work a bit with your data, but the hold on, hold off is what you need:
windspeed_user = input('please enter a 5 windspeed vector'); %requesting input from user.
i = 1;
for i = 1:5;
W_2 = Windchill_calc(temp_vector, windspeed_user(i)) %calls function
hold on
plot (W_2, temp_vector)
end
hold off
xlabel ('Temperature (Fahrenheit)')
ylabel ('Windchill results')
grid on
  댓글 수: 1
Aaron
Aaron 2013년 12월 17일
편집: Image Analyst 2013년 12월 18일
Yeah your right I do - I'm now trying to embellish a little, do you know why this if statement isn't working? (I want to limit the amount of values user inputs)
This is the result after adding the 'hold' feature, thank you for your fast reply.

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


Walter Roberson
Walter Roberson 2013년 12월 17일
Each iteration of the loop, you are over-writing all of W_2, so at the end of the loop only the final assignment has had any effect.
It appears to me that your W_2 would be a vector. If so, then
for i = 1 : 5
W_2 = Windchill_calc(temp_vector, windspeed_user(i)) %calls function
plot(temp_vector, W_2);
hold on
end
  댓글 수: 2
Aaron
Aaron 2013년 12월 17일
Mm, that exactly what was happening - because as you said its the last result stored.
The hold function solved it instantly, :). Thankyou for your help and quick reply.
Walter Roberson
Walter Roberson 2013년 12월 17일
Notice I also switched W_2 and temp_vector, as the independent variable should go first in the plot()

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by