How do I stop a function?

18 ビュー (過去 30 日間)
Renato Pereira
Renato Pereira 2017 年 9 月 11 日
コメント済み: Walter Roberson 2017 年 9 月 13 日
I have a function running when I push a button in a GUI. I want to end this function when I press another button. example:
function first_callback (hObject, eventdata, handles)
a = b + c
while...
...
...
...
end
end
function stopbuttonback_callback (hObject, eventdata, handles)
%something that stops function first_callback
end

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 12 日
function first_callback (hObject, eventdata, handles)
a = b + c
set(handles.stopbutton, 'UserData', false);
while true
drawnow();
if get(stopbutton, 'UserData'); break; end
...
...
...
end
end
function stopbuttonback_callback (hObject, eventdata, handles)
set(hObject, 'UserData', true);
end
  3 件のコメント
Renato Pereira
Renato Pereira 2017 年 9 月 13 日
Dont know why but when I write the "while true" the function stopbuttonback_callback doesn't work... let me give you the all code:
function start_Callback(hObject, eventdata, handles)
a = 0;
while a < 100
b = datestr(a/(24*60*60),'HH:MM:SS');
set(handles.time,'String',b);
a = a + 1;
pause(1)
end
function stop_Callback(hObject, eventdata, handles)
set(handles.time,'String','00:00:00');
Walter Roberson
Walter Roberson 2017 年 9 月 13 日
function start_Callback(hObject, eventdata, handles)
a = 0;
set(handles.stop, 'UserData', false);
while a < 100
pause(1); %pause works as well as drawnow() to allow interrupts
if get(handles.stop, 'UserData'); break; end
b = datestr(a/(24*60*60),'HH:MM:SS');
set(handles.time,'String',b);
a = a + 1;
end
function stop_Callback(hObject, eventdata, handles)
set(handles.time,'String','00:00:00');
set(hObject, 'UserData', true);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by