How to create a Stop button in matlab

Hi I want to make a Stop button in my matlab code so that when I press it then the code stops running.. Lets say that I have this code in my program.m (a part of my whole code).
while true
for k=1:10
matrix{k}=cell(10,10);
end
for i=1:10
[Err_Code Function O1 O2 O3 O4 O5 O6 Array Len] = call('Apin','Apin',Product,Channel,'A-scan',Type_Don,0,Nb_Echa,Forme_Onde,0,0,O1,O2,O3,O4,O5,O6,Array,Len);
[matrix{i,:}]= deal(Array);
end
pause(2);
end
Now I want to create a stop button in my program.m file to stop the program when I want to. I did google about that and I found these code lines to be used:
button = uicontrol('Style','text','String','Stop');
set(gcbo,''userdata'',1);
set(gcbo,''userdata'',0);
if get(button ,'userdata') % stop condition
break;
end
I do not know how to use these code lines in my program.m? I do not understand what userdata stands for? Can you tell me where should I put those code lines in my program.m?
Thanks

回答 (2 件)

Image Analyst
Image Analyst 2017 年 2 月 23 日

2 投票

I do exactly (and only) that in this simple demo I'm attaching. Press the Go button and it counts up and displays the count in a label. Press the Stop button and you'll see the counting stop.

3 件のコメント

Liza Vörös
Liza Vörös 2020 年 3 月 23 日
This does not work
Image Analyst
Image Analyst 2020 年 3 月 23 日
It definitely does work. I just tried it. You must have modified it somehow. Attach your modified code.
Dara Singh
Dara Singh 2022 年 2 月 9 日
Put both gostop.fig and gostop.m files in your directory.

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

Jos (10584)
Jos (10584) 2017 年 2 月 23 日

0 投票

Take a look at my (old) STOPLOOP function on the File Exchange:

1 件のコメント

ali alkhudri
ali alkhudri 2017 年 2 月 23 日
it is not clear how to do for me?

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

カテゴリ

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

タグ

質問済み:

2017 年 2 月 23 日

コメント済み:

2022 年 2 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by