Issues with Breaking while loop in Appdesigner.
Show older comments
I need to break while loop - which reads a sensor value - I want to break the while loop when pushbutton is pressed.
I tried using "Tag" handle in pushbutton to break the loop - it breaks loops before pushing the pushbutton.
Error:
Error using matlab.ui.control.Button/get
Unrecognized property Value for class Button.
Error in IRgauge/ReadDistanceButtonPushed (line 36)
stop_state = get(app.CLOSEButton,'Value');
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 382)
Error while evaluating Button PrivateButtonPushedFcn.
Code for reading sensor in appdesigner "Code View"
function ReadDistanceButtonPushed(app, event)
CommPort = serialport('COM30',9600);
while 1
R = readline(CommPort);
R = str2double(R);
app.DISTANCEGauge.Value = R;
drawnow();
stop_state = get(app.CLOSEButton,'Value'); % to break infinity loop
if stop_state
break;
end
end
end
% callback function breaking infinity loop
function CLOSEButtonPushed2(app, event)
close all; clc; delete(instrfind);
end
5 Comments
Mario Malic
on 29 Oct 2020
Try this,
function ReadDistanceButtonPushed(app, event)
CommPort = serialport('COM30',9600);
while 1
R = readline(CommPort);
R = str2double(R);
app.DISTANCEGauge.Value = R;
drawnow();
if app.CLOSEButton.Value; % to break infinity loop
break;
end
end
% Clean stuff here
close all; clc; delete(instrfind);
end
Ameer Hamza
on 30 Oct 2020
The problem is that the push button in MATLAB does not have a property named 'Value', so the statement
app.CLOSEButton.Value
will cause an error.
Mario Malic
on 30 Oct 2020
I have to work on this part of carefully reading the whole question. That's correct, push button does not have the Value property. The better solution would be State Button, which has one, thus having only one button on UI.
Ameer Hamza
on 30 Oct 2020
I agree, state-button seems more suitable for the thing OP is trying to do.
Franck paulin Ludovig pehn Mayo
on 12 Jan 2022
@Ameer Hamza hello , please could you have a look on the link below.
Answers (1)
Ameer Hamza
on 29 Oct 2020
1 vote
Try the method in the attached app.
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!