GUIData无法正确更新 [英] GUIData not updating properly

查看:88
本文介绍了GUIData无法正确更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一个包含计时器的GUI.我希望每次调用计时器时都将索引加1,并将其存储在guidata中.我希望功能在需要时可以向后移动,因此仅使用TasksExecuted字段将无法工作.我的问题是索引根本不会增加.这是计时器的声明

I have a GUI in MATLAB which contains a timer. I want every time the timer is called to increment an index by one, and store it in the guidata. I would like functionality to go backwards if needed, so just using the TasksExecuted field would not work. My problem is that the index simply does not increment. This is the declaration of the timer

handles.index= 1 ;
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
'Period', 1, ...                % Initial period is 1 sec.
'TimerFcn', {@update_display,hObject,handles}); % Specify callback

这是函数的相关部分.

function update_display(hObject,eventdata,hfigure,handles)
tin = evalin('base','t_in');
curtime = tin.time(handles.index);
fprintf('%f',handles.index);
index = handles.index;

...

handles.index = index+1
guidata(handles.figure1,handles);

Debug语句表示该函数的末尾索引始终为2.我在这里做什么错了?

Debug statements say that the index will always be two at the end of the function. What am I doing wrong here?

谢谢.

推荐答案

向回调函数提供输入变量时,调用回调时传递的变量是定义了回调时存在的变量.您可以通过一个简单的示例看到它:

When providing an input variable to a callback function, the variable that is passed when you invoke the callback is that variable as it exists when the callback is defined. You can see this with a simple example:

function testcode
handles.mainwindow = figure();

handles.idx = 1;

handles.button1 = uicontrol('Style','pushbutton', 'String', 'Button1', ...
    'Units','normalized', 'Position', [0.05 0.05 .30 .90], ...
    'Callback', {@button, handles} ...
    );
handles.button2 = uicontrol('Style','pushbutton', 'String', 'Button2', ...
    'Units','normalized', 'Position', [0.35 0.05 .30 .90], ...
    'Callback', {@button, handles} ...
    );
handles.button3 = uicontrol('Style','pushbutton', 'String', 'Button3', ...
    'Units','normalized', 'Position', [0.65 0.05 .30 .90], ...
    'Callback', {@button, handles} ...
    );
end

function button(hObj,~,handles)
fprintf('Initial idx: %u\n', handles.idx);
handles.idx = handles.idx + 1;
guidata(hObj, handles);
tmp = guidata(hObj);
fprintf('Stored idx: %u\n', tmp.idx);
end

按下每个按钮并查看显示的输出.

Push each button and check out the displayed output.

要解决此问题,您可以利用update_display中的guidata来获取和存储您的handles结构,而不是显式传递它:

To get around this, you can utilize guidata in update_display to get and store your handles structure rather than passing it explicitly:

handles.index = 1;
handles.timer = timer( ...
'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
'Period', 1, ...                % Initial period is 1 sec.
'TimerFcn', {@update_display,hObject}); % Specify callback
guidata(handles.figure1, handles);

function update_display(hObject,eventdata,hfigure)
handles = guidata(hfigure);
guidata(handles.figure1, handles);
tin = evalin('base','t_in');
curtime = tin.time(handles.index);
fprintf('%f',handles.index);
index = handles.index;

% ... do things

handles.index = index+1
guidata(handles.figure1,handles);

如果这是一个GUIDE GUI,则修改handles结构可能会产生意想不到的后果.我建议改用 setappdata

If this is a GUIDE GUI, modifying the handles structure can have unintended consequences. I'd recommend instead using setappdata and getappdata:

setappdata(hObject, 'index', 1);  % Assuming hObject is your figure window
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
'Period', 1, ...                % Initial period is 1 sec.
'TimerFcn', {@update_display,hObject}); % Specify callback

function update_display(hObject,eventdata,hfigure)
index = getappdata(hfigure, 'index');
% ... do things
index = index + 1;
setappdata(hfigure, 'index', index);

这篇关于GUIData无法正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆