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

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

답변 (1개)

Yongjian Feng
Yongjian Feng 2021년 8월 8일
편집: Yongjian Feng 2021년 8월 8일
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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 8월 8일

편집:

2021년 8월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by