Plotting the updated values from if statement under for-loop

...................... % lines of code
...................... % lines of code
n = 70
dt = 0.1 % 0.1second increment
for i = 1:n
t = dt*(i-1); % elapsed time
................. % lines of code
................. % lines of code
if (condition)
tipper(A,B,r); % calling a function called tipper that outputs delta_q
elapsed_time = t
plot(elapsed_time, delta_q)
else
fprintf('Not happening\n');
end
end
Whenever the first condition is satisfied, I need to plot delta_q (output from a function called tipper) vs. elapsed_time for all i values from 1 to n (I believe there will be gaps in the plot where the 'Not happening' occurs). The 'plot(elapsed_time, delta_q)' part in the code doesn't seem to work.

 Réponse acceptée

KSSV
KSSV le 11 Juil 2017
Modifié(e) : KSSV le 12 Juil 2017
Use
plot(elapsed_time, delta_q,'.')
instead of
plot(elapsed_time, delta_q)
Note that you are plotting a single point at instance, so you should plot it by a marker. Also, you need to use hold on after the plot or, before the loop figure; hold on.
You have other way to achieve this, below is the pseudo code for that. I would suggest you to follow this method.
...................... % lines of code
...................... % lines of code
n = 70
dt = 0.1 % 0.1second increment
xdata = NaN(1,n) ;
ydata = NaN(1,n) ;
for i = 1:n
t = dt*(i-1); % elapsed time
................. % lines of code
................. % lines of code
if (condition)
tipper(A,B,r); % calling a function called tipper that outputs delta_q
elapsed_time = t ;
xdata(i) = elapsed_time ;
ydata(i) = delta_q ;
else
fprintf('Not happening\n');
end
end
plot(elapsed_time, delta_q)

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by