2014-12-04 153 views
0

有时我的程序运行时间过长,所以我想知道用户是否可以在图形界面上随时停止程序。如何在MatLab中的图形界面中停止执行

我试过了,但是当程序运行一个函数时它不会读取另一个函数(例如,用户会说它停止的一个函数)。

回答

1

如果你真的想要一个像stop这样的按钮,唯一的选择就是在长时间运行的过程中执行检查,经常询问是否应该停止。

一个微小的反例:

function teststop 
    f = figure('pos', [0,0,200,100],... 
     'menubar', 'none',... 
     'toolbar', 'none'); 
    movegui(f, 'center'); 

    c = uiflowcontainer(f, 'FlowDirection', 'topdown'); 
    uicontrol(c, 'style', 'pushbutton', 'string', 'start', 'callback', @start); 
    uicontrol(c, 'style', 'pushbutton', 'string', 'stop', 'callback', @stop); 
end 

function start(hObject,~) 
    fig = ancestor(hObject, 'figure'); 
    setappdata(fig, 'stop', false); 

    % disable another start 
    set(hObject, 'Enable', 'inactive');  

    count = 0; 
    % increment counter as long as we're not told to stop 
    while ~getappdata(fig, 'stop') 
     count = count+1; 
     % a tiny pause is needed to allow interrupt of the callback 
     pause(0.001); 
    end 
    fprintf('Counted to: %i\n',count); 

    % re-active button 
    set(hObject, 'Enable', 'on'); 
end 

function stop(hObject, ~) 
    disp('Interrupting for stop'); 
    % set the stop flag: 
    setappdata(ancestor(hObject, 'figure'), 'stop', true);  
end 

它只是保存到teststop.m和运行。 请注意,在任何情况下都需要pause(0.001)以允许回拨中断。如果没有暂停呼叫,以上方法无法工作。

当然,check-stop-stop需要时间,所以我建议不要过于频繁地检查。或者,如果您处理的是周期性的事情,比如等待输入或其他事情发生,您可以使用timer来实现它,这可以轻松停止。

0

Matlab中的停止运行命令是ctrl + c或ctrl + break,但是如果你的程序导致Matlab崩溃,它可能不需要这些命令,你将不得不强制关闭程序。在运行程序时,在命令窗口中尝试ctrl + c,它应该停止执行。

嗯......图形界面花费的时间太长了吗?您可以尝试在代码之间添加中断以确定什么会减慢流程。在开始处添加tic,并在您想要定时的地方添加tic。

+0

我认为这个问题是针对图形界面的用户的。他不能按ctrl + c ... – Adiel 2014-12-04 09:27:35