MATLAB Answers

How do I end this Matlab App Designer switch callback?

36 ビュー (過去 30 日間)
Stylianos Vamvakas
Stylianos Vamvakas 2020 年 5 月 31 日
コメント済み: Kojiro Saito 2020 年 6 月 2 日
I have a switch that I want to let the piston fire to change between 0 and 1 while its on and then end the loop when its off. No matter what I am unable to break the loop.
function FireSwitchValueChanged(app, event)
value = app.FireSwitch.Value;
while strcmp(value,"On")
app.piston_state = 1;
disp(app.piston_state)
pause(0.05)
app.piston_state = 0;
disp(app.piston_state)
drawnow %didnt change anything
if strcmp(value,"Off")
app.piston_state = 0;
disp(app.piston_state)
break
end
end

  0 件のコメント

サインインしてコメントする。

採用された回答

Kojiro Saito
Kojiro Saito 2020 年 6 月 1 日
編集済み: Kojiro Saito 2020 年 6 月 1 日
Updated answer
I understand what you're looking for. You can implement flashing lights using timer class.
First, you need to create a private property.
and name the timer property, say timerObj.
properties (Access = private)
timerObj % Description
end
Then, you need to create a private function.
and name the function like myTimerFcn and define it as the following.
methods (Access = private)
function myTimerFcn(app)
if app.piston_state == 0
app.piston_state = 1;
else
app.piston_state = 0;
end
disp(app.piston_state)
end
end
Next, add startupFcn from Callbacks tab.
and define it as follows. It creates a timer class.
function startupFcn(app)
app.timerObj = timer;
app.timerObj.TimerFcn = @(~, ~)myTimerFcn(app);
app.timerObj.Period = 0.1;
app.timerObj.ExecutionMode = 'fixedSpacing';
end
Finally, you can call this timer from swithc value changed function.
function FireSwitchValueChanged(app, event)
value = app.FireSwitch.Value;
if strcmp(value,"On")
start(app.timerObj)
else
stop(app.timerObj)
end
end
I hope this work!
--
Original answer
Switch value changed functions are called once when the switch is pushed, so I don't think while loop is necessary in your FireSwitchValueChanged.
function FireSwitchValueChanged(app, event)
value = app.FireSwitch.Value;
if strcmp(value,"On") % or, if value == "On" is also OK.
app.piston_state = 1;
disp(app.piston_state)
else
app.piston_state = 0;
disp(app.piston_state)
end
end
Does this meet your requirement?

  4 件のコメント

表示 1 件の古いコメント
Kojiro Saito
Kojiro Saito 2020 年 6 月 1 日
Thank you for your comment. I've updated my previous answer.
Stylianos Vamvakas
Stylianos Vamvakas 2020 年 6 月 1 日
Thank you very much! This works perfect
Kojiro Saito
Kojiro Saito 2020 年 6 月 2 日
Great to hear that! Please accept my answer if you can.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by