3

My Matlab GUI is a form with many text fields that are initially populated using the same data struct. Each text field has a callback and a create function, where the text field is assigned the value of the given 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. I have tried refresh(h) and drawnow(), with no luck.

Now my question is whether or not there is such a function built into the matlab guide framework?

am304
  • 13,758
  • 2
  • 22
  • 40
user1186155
  • 238
  • 2
  • 10

2 Answers2

1

When you set a property of a handle like set(h,'String',str), the value of str is copied. 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.

Calling guidata(hObject, handles); is only for updating the GUI with modifications to handles. 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. In your GUI's opening function, create a timer that defines an update function to run periodically:

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!

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • Thanks! That pretty much answered my question...seems like the textfield callbacks are only called upon user input, or are there other situations too? – user1186155 Dec 10 '13 at 21:07
  • 1
    @user1186155 The rules for triggering a callback are different for each object type, but yeah, the callbacks don't just run automatically. However, you can create a `timer` that will update the callback periodically at a certain interval of time. I updated the answer with a suggestion along these lines. – chappjc Dec 10 '13 at 22:11
0

You need to update the handles structure with this :

% Update handles structure
guidata(hObject, handles);
Vuwox
  • 2,331
  • 18
  • 33
  • I have tried that too, however it seems with no effect, while only using 'set' seemed to work. Maybe I'm referring to the wrong handle? My first guess would be that the handle hObject should be the same as h in my code, i.e. the handle to the form. However, traces in the code indicate that the callbacks associated with each textfield are not called when calling guidata(h, handles). – user1186155 Dec 10 '13 at 20:32
  • All right. Maybe I did not quite understand then. Your textfield get fill with string and they are display. But you just want to get something recursive to fill them ? I'm not sure to catch it. – Vuwox Dec 10 '13 at 21:00