[英]Updating all components of a Matlab gui (created with guide)
My Matlab GUI is a form with many text fields that are initially populated using the same data struct. 我的Matlab GUI是一个包含许多文本字段的表单,最初使用相同的数据结构填充。 Each text field has a callback and a create function, where the text field is assigned the value of the given struct. 每个文本字段都有一个回调和一个create函数,其中text字段被赋予给定struct的值。 However, at some later point I would like to repopulate the form using a different struct, as an event triggered by pressing a push button. 但是,稍后我想使用不同的结构重新填充表单,作为按下按钮触发的事件。 The code looks approximately like: 代码看起来大致如下:
h = MyFigure;
global mystruct
mystruct = somevalues;
handles = guidata(h);
set( handles.textfield1, 'String', mystruct.value1 )
...
set( handles.textfieldN, 'String', mystruct.valueN )
However, if I could make Matlab call all these callbacks recursively (like a "validate tree" function), I wouldn't have to call "set" for each text field. 但是,如果我可以让Matlab递归调用所有这些回调(比如“验证树”函数),我就不必为每个文本字段调用“set”。 I have tried refresh(h) and drawnow(), with no luck. 我试过刷新(h)和drawow(),没有运气。
Now my question is whether or not there is such a function built into the matlab guide framework? 现在我的问题是matlab指南框架中是否有这样的功能?
When you set
a property of a handle like set(h,'String',str)
, the value of str
is copied. 当你set
像set(h,'String',str)
这样的句柄属性set(h,'String',str)
会复制str
的值。 It is not a reference to that variable that can be updated automatically. 它不是可以自动更新的变量的引用。 Your best bet is to make a subroutine called updateText
or something like that, put all of the set
statements in it, and call it when needed. 最好的办法是创建一个名为updateText
的子程序或类似的东西,将所有的set
语句放入其中,并在需要时调用它。
Calling guidata(hObject, handles);
调用guidata(hObject, handles);
is only for updating the GUI with modifications to handles
. 仅用于通过修改handles
来更新 GUI。 You may need this elsewhere, but for the job of updating properties of certain handle graphics objects, it is not really used. 您可能在其他地方需要这个,但是为了更新某些句柄图形对象的属性,它并没有真正使用。
One possibility is to create a timer
to update the text fields on a regular basis. 一种可能性是创建timer
以定期更新文本字段。 In your GUI's opening function, create a timer that defines an update function to run periodically: 在GUI的打开功能中,创建一个定时器,定义一个定期运行的更新功能:
T = timer('Period',1,'StartDelay',0.5,'TimerFcn', ...
{@updateTextBoxes,handles},'ExecutionMode','FixedRate');
start(T)
The update function would look like: 更新功能如下所示:
function updateTextBoxes(hTimerObj, timerEvent, handles)
global mystruct
% get mystruct data however you do it...
% maybe also get handles via handles=guidata(hTimerObj); instead of input
set( handles.textfield1, 'String', mystruct.value1 )
...
set( handles.textfieldN, 'String', mystruct.valueN )
EDIT : Do NOT forget to delete the timer ( delete(T)
) or stop it before you quit the GUI or do clear T
, otherwise it will just keep running and you will have to quit MATLAB... No, I did not just do this myself! 编辑 :不要忘记删除计时器( delete(T)
)或在退出GUI之前停止它或clear T
,否则它将继续运行,你将不得不退出MATLAB ...不,我不只是自己这样做!
You need to update the handles structure with this : 您需要使用以下命令更新句柄结构:
% Update handles structure
guidata(hObject, handles);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.