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

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

Answers (1)

Yongjian Feng
Yongjian Feng on 8 Aug 2021
Edited: Yongjian Feng on 8 Aug 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

Categories

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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!