在Matlab中从GUI切换for循环的索引

在Matlab中从GUI切换for循环的索引,matlab,user-interface,Matlab,User Interface,我想创建一个带有两个按钮的GUI。一个代表下一个,另一个又回来了。这两个按钮应该切换for循环的索引。例如,当我按下“下一步”按钮时,它应该转到下一次迭代(I+1),然后按下“后退”,它就会转到(I-1)。。我非常感谢你的回答 您将无法从GUI更改循环索引。 更好的方法是使用while循环,等待其中一个按钮,然后从图中获取新索引: hFig = figure(..) % create your figure somewhere here setappdata(hFig, 'loopIndex',

我想创建一个带有两个按钮的GUI。一个代表下一个,另一个又回来了。这两个按钮应该切换for循环的索引。例如,当我按下“下一步”按钮时,它应该转到下一次迭代(I+1),然后按下“后退”,它就会转到(I-1)。。我非常感谢你的回答

您将无法从GUI更改循环索引。 更好的方法是使用while循环,等待其中一个按钮,然后从图中获取新索引:

hFig = figure(..) % create your figure somewhere here
setappdata(hFig, 'loopIndex', 0); % assuming you start at 0
while 1:
    uiwait(hFig); % this will wait for anyone to call uiresume(hFig)
    loopIndex = getappdata(hFig, 'loopIndex');
    % do whatever you're doing with the index
    % ...
    % ...
    % stop loop at some point:
    if <end-condition>: % pseudo-code, obviously
        break 

我不建议使用for循环,但建议使用while循环。因为您确实不想为例如1000个步骤运行代码,但您希望在I介于2个值之间时执行该操作。所以你的循环中会有一个catch行,下一个按钮返回+1,上一个按钮返回-1。但要注意不要编程无休止的循环,不要考虑断点/停止点。
function button_callback(hObject, evt, handles):
    hFig = ancestor(hObject, 'figure');
    % increment the loop index:
    setappdata(hFig, 'loopIndex', 1+getappdata(hFig, 'loopIndex'))
    % proceed with the loop code
    uiresume(hFig);