Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Answers (2)

David Sanchez
David Sanchez on 17 Dec 2013
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 Comment
Aaron
Aaron on 17 Dec 2013
Edited: Image Analyst on 18 Dec 2013
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.

Sign in to comment.


Walter Roberson
Walter Roberson on 17 Dec 2013
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 Comments
Aaron
Aaron on 17 Dec 2013
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 on 17 Dec 2013
Notice I also switched W_2 and temp_vector, as the independent variable should go first in the plot()

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!