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

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

 採用された回答

Jan
Jan 2020 年 11 月 24 日
編集済み: Jan 2020 年 11 月 24 日
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 件のコメント

shubham kumar gupta
shubham kumar gupta 2020 年 12 月 1 日
編集済み: shubham kumar gupta 2020 年 12 月 2 日
can you help me I tried this
2nd pic: In appdesigner Inside a callback to a button "PLOT AND SIM" i passed a function main_plot_function()
1st pic: shows that main_plot_function Now "STOP BUTTON " appears but clicking on that it doesnt stops loop of plotting nor it print "BUTTON!!"
IT WORKS IN DEBUG MODE BUT NOT IN NORMAL MODE
shubham kumar gupta
shubham kumar gupta 2020 年 12 月 1 日
編集済み: shubham kumar gupta 2020 年 12 月 2 日
Works only in Debug mode and not normal mode.. why is it so?
cool solved I increased the pause time!! :) but Thats what actually happens when I click Breakpoints stop
Somehow It working!
Jan
Jan 2020 年 12 月 7 日
Hint: Avoid global variables, because they are a shot in your knee.

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

その他の回答 (1 件)

Voss
Voss 2020 年 11 月 23 日
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 件のコメント

problem is there are two functions one is RUN and another is BUTTON PRESS
RUN CONTAINS PLOT CODE
AND I WANT TO BREAK FROM IT VIA BUTTON PRESS
Voss
Voss 2020 年 11 月 23 日
Your variable that says whether the button has been clicked (let's call it "is_button_clicked" for the sake of discussion) will need to be available to both functions. There are a number of ways to achieve this:
1) you can store is_button_clicked in the handles structure of your GUI (if you are using GUIDE, this structure is accessible via guidata())
2) you can make RUN() and BUTTON_PRESS() nested under another function that contains is_button_clicked
3) you can make is_button_clicked a global variable
shubham kumar gupta
shubham kumar gupta 2020 年 11 月 23 日
編集済み: shubham kumar gupta 2020 年 11 月 24 日
My final aim is to get a standalone app with pause and start feature of plot so I need to break this loop on button click
I tried this but not worked
function RUN_PLOT()
time=1:0.01:3600;
figure
hold on;
global flag;
i=1;
flag=true;
while(flag && i<numel(t))
y(1,i)=readValue(
plot(t(1:i),y(1:i));
pause(0.02)
i=i+1;
end
end
on Button click I did this
function BUTTON_PRESS()
global flag
flag=false
end
Do not use a global. It is much better to share data through guidata or by making them nested functions. You can even set the flag value as the UserData of a GUI element.
All other functions can access and overwrite global variable contents. Especially with such a short name as flag this risk is non-zero. If you really insist on using global variable, make the name as long as you can: include the function name and include a long description. You have 63 characters, use them.
global RUN_PLOT_GUI___flag_to_stop_plotting_loop_if_true
shubham kumar gupta
shubham kumar gupta 2020 年 12 月 1 日
編集済み: shubham kumar gupta 2020 年 12 月 1 日
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 2020 年 12 月 1 日
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?

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 11 月 23 日

コメント済み:

Jan
2020 年 12 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by