How do I make my program KEEP doing a command at a certain condition?
Afficher commentaires plus anciens
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
Réponses (1)
Yongjian Feng
le 8 Août 2021
Modifié(e) : Yongjian Feng
le 8 Août 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
Catégories
En savoir plus sur Logical 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!