我知道 Matlab 有可能通过使用 input()
暂停 Matlab 函数或脚本,因此当我有一个 for 循环并想在每次迭代时暂停它:
for i = 1:N
reply = input(sprintf('Pausing in iteration %d! Continue with Enter!', i), 's');
% Calculations
end
但如何使以下工作正常进行:通常它会在不暂停的情况下运行(好像 input() 不会在那里)但是当用户做某事时(可能按下一个键、一个按钮或 matlab 中的类似东西) ,脚本/函数会在每次迭代时暂停,直到用户再次执行它,因此是这样的。例如。假设 matlab 有可能在按下某个热键组合时切换变量,例如Ctrl+Alt+A 在 0 和 1 之间切换变量 myToggle
我可以轻松做到:
for i = 1:N
if(myToggle == 1)
reply = input(sprintf('Pausing in iteration %d! Continue with Enter!', i), 's');
end
% Calculations
end
提前致谢!
编辑:刚刚在这里找到了一种可能的解决方案:Function to ask a MATLAB program to wait for an event before continuing execution我可以在我的函数/脚本的开头创建一个文件,并在它不再存在时在下一次迭代时暂停。但这将需要用户重命名不太“舒服”的文件......还有其他想法吗? :)
最佳答案
您可以使用带有按钮的简单 GUI;一旦按下,执行将在下一次迭代中停止。
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 - 在某些用户事件上暂停 Matlab 脚本或函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6472731/