How do I make my program KEEP doing a command at a certain condition?

1 visualización (últimos 30 días)
Ryan Mills
Ryan Mills el 8 de Ag. de 2021
Editada: Yongjian Feng el 8 de Ag. de 2021
Hi,
I was wondering how I could make my program do an action at a certain condition for a certain amount of time. For example, how do I make it so if Val is less or equal to 20, action 1 keeps getting done for a certain amount of time. In this case until Val is 30. I am a beginner at this so my apologies if this sounds confusing.
Thanks!
if Val <= 20
%action1
while Val == 30
%action2
end
else
%action3
end

Respuestas (1)

Yongjian Feng
Yongjian Feng el 8 de Ag. de 2021
Editada: Yongjian Feng el 8 de Ag. de 2021
Try this:
if Val <= 20
max_time = 120; % assume do it for at most 2 mins=120secs
wait_time = 10; % wait for 10 sec. So at most 120/10=12 times
for i=1:max_time/wait_time
% do your action 1
% now you need to update Val
if Val > 20
% done
break;
end
pause(wait_time); % wait for sometime
end
% When you are here, either Val is > 20 or you run 12 times already but
% Val is still smaller than 20. What do you want here?
else
%action3
end

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by