Break loop with button click Appdesigner GUI [Both plot and button are in different callback functions]

18 vues (au cours des 30 derniers jours)
I have a GUI MATLAB
I have a button
and there is a loop like this
function RUN()
t=1:0.01:3600;
for i=1:numel(t)
y(1,i)=readValue();
plot(t(1:i),Tco(1:i));
pause(0.02)
end
end
function BUTTON_PRESS()
%BREAK FROM THAT LOOP
end
I want to break this loop when I click my button

Réponse acceptée

Jan
Jan le 24 Nov 2020
Modifié(e) : Jan le 24 Nov 2020
function main
FigH = figure;
ButtonH = uicontrol(FigH, 'Style', 'PushButton', 'String', 'Stop',
'Callback', @ButtonPressed, 'UserData', 0);
RUN(ButtonH);
end
function ButtnPressed(ButtonH, EventData)
ButtonH.UserData = 1;
end
function RUN(ButtonH)
t = 1:0.01:3600;
for i = 1:numel(t)
y(1,i) = readValue();
plot(t(1:i), Tco(1:i));
pause(0.02);
if ButtonH.UserData
break;
end
end
end
If there is really no chance to obtain a clean providing of the button's handle, use the tag to find it dynamically - but this is less nice:
function main
FigH = figure;
ButtonH = uicontrol(FigH, 'Style', 'PushButton', 'String', 'Stop', ...
'Callback', @ButtonPressed, 'UserData', 0, ...
'Tag', 'myUniqueButtonTag_in: main.m');
RUN();
end
function ButtnPressed(ButtonH, EventData)
ButtonH.UserData = 1;
end
function RUN()
ButtonH = findobj(allchild(groot), 'Tag', 'myUniqueButtonTag_in: main.m');
... same as above
end
Care for the situation, where two figures are existing or none, e.g. by:
function RUN()
ButtonH = findobj(allchild(groot), 'Tag', 'myUniqueButtonTag_in: main.m');
if numel(ButtonH) > 1
warning('Multiple stop buttons found.')
end
...
if ~isempty(ButtonH) && any([ButtonH.UserData])
end
  4 commentaires
shubham kumar gupta
shubham kumar gupta le 2 Déc 2020
cool solved I increased the pause time!! :) but Thats what actually happens when I click Breakpoints stop
Somehow It working!
Jan
Jan le 7 Déc 2020
Hint: Avoid global variables, because they are a shot in your knee.

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 23 Nov 2020
You can define a variable that says whether the button has been clicked. Set the value of that variable in your button's callback function. Check the value of that variable on each iteration of your for loop, and if it says the button has been clicked, break out of the loop.
  6 commentaires
shubham kumar gupta
shubham kumar gupta le 1 Déc 2020
Modifié(e) : shubham kumar gupta le 1 Déc 2020
NO HELPED!! Tried unable to stop loop even changing the name flag to RUN_PLOT_GUI___flag_to_stop_plotting_loop_if_true!!
Rik
Rik le 1 Déc 2020
Changing the name will not solve anything if your code is not working in the first place. It is a good habit.
Instead of holding down the shift button while typing on this forum (which is considered SHOUTING and impolite), did you try using breakpoints to debug your code?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by