在某些用户事件上暂停Matlab脚本或函数 [英] Pausing Matlab Script or Function on certain user event
问题描述
我知道Matlab有可能通过使用input()
暂停Matlab函数或脚本,因此当我有一个for循环并想在每次迭代中暂停它时:
I know that there is the possibility of Matlab of pausing a Matlab function or script by using input()
thus when I have a for-loop and want to pause it every iteration:
for i = 1:N
reply = input(sprintf('Pausing in iteration %d! Continue with Enter!', i), 's');
% Calculations
end
但是如何实现以下工作:通常,它在运行时不会暂停(就像input()不会在那儿一样),而是在用户执行某项操作(例如按matlab中的某个键,一个按钮或类似操作)时运行,脚本/函数会暂停每次迭代,直到用户再次执行它为止,因此是这样的.例如.假设当按下某个特定的热键组合时,matlab有可能切换变量. Ctrl + Alt + A可以将变量myToggle
在0和1之间切换,我可以很容易地做到这一点:
but how to get the following working: Usually it runs without pausing (as if the input() wouldn't be there) but when the user does something (perhaps press a key, a button, or something similar in matlab), the script/function does PAUSE each iteration until the the user does it again, thus something like this. E.g. let's say there is the possibility of matlab to toggle a variable whenh pressing a certain hotkey combination, e.h. Ctrl+Alt+A toggles the variable myToggle
between 0 and 1 I could easily do:
for i = 1:N
if(myToggle == 1)
reply = input(sprintf('Pausing in iteration %d! Continue with Enter!', i), 's');
end
% Calculations
end
提前谢谢!
EDIT: Just found ONE possible solution here: Function to ask a MATLAB program to wait for an event before continuing execution I could create a file on the beginning of my function/script and pause on the next iteration when it doesn't exist anymore. But that would require the user to rename the file which isn't quite that 'comfortable'... Any other ideas? :)
推荐答案
您可以使用带有按钮的简单GUI;一旦按下,将在下一次迭代中暂停执行.
You can use a simple GUI with a button; once pressed the execution halts on the next iteration.
function testGUI()
doPause = false;
figure('menu','none', 'Position',[400 400 150 30])
hb = uicontrol('Style','pushbutton', 'String','Pause', ...
'Callback',@buttonCallback, 'Unit','Normalized', 'Position',[0 0 1 1]);
%drawnow
while ishandle(hb)
%# check if the user wants to pause
if doPause
pause() %# pauses until user press any key
%# reset
doPause = false;
if ishandle(hb), set(hb, 'Enable','on'), drawnow, end
end
%# Calculations
disp(rand), pause(.1)
%# keep the GUI responsive
drawnow
end
%# callback function for the button
function buttonCallback(hObj,ev)
doPause = true;
set(hObj, 'Enable','off')
end
end
这篇关于在某些用户事件上暂停Matlab脚本或函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!