guidata doesn't work (the way I expected it to)

1 view (last 30 days)
I made a GUI using guide that utilizes the 'handles' structure store and pass important values. The problem I'm having is when I compute some values in a function, add them to the handles structure, then try to update the structure to the main GUI workspace it doesn't do it. In the scope of the function I am running the 'handles' structure updates, but then guidata(handles.mainGuiHandle,handles) doesn't seem to have any effect - that is, when I look at the 'handles' structure after the function has finished running the new values are gone. Here is a cartoon of what happens:
Suppose I have a GUI that whos 'handles' structure contains:
handles.mainGuiHandle
handles.anotherHandle
handles.aDifferentHandle
handles.data
handles.differentData
Suppose I use a function 'calculate' that looks like this:
function calculate(handles);
data = handles.data;
differentData = handles.differentData;
newData = data.*differentData;
handles.newData = newData;
guidata(handles.mainGuiHandle,handles);
If I run debugger and look at the 'handles' structure before guidata executes I can see that 'handles' updates and becomes
handles.mainGuiHandle
handles.anotherHandle
handles.aDifferentHandle
handles.data
handles.differentData
handles.newData
But then when the function finishes running if I look at 'handles' in the main GUI workspace it is still the unupdated version. I expected that the 'guidata' function would have updated 'handles' in the scope of the main GUI. What am I missing?

Answers (2)

Walter Roberson
Walter Roberson on 24 Jun 2011
Just don't expect that ;-)
What gets updated when you use guidata() is data cached away in a property of a figure object. The routines fetch copies of that data as it was as of the time they made the call. Unless routines specifically request a new copy, they will not get the updated version.

Matt Fig
Matt Fig on 24 Jun 2011
The handles structure and the stuff stored in GUIDATA are not the same thing. If you want to be able to store new stuff in a structure, then in each callback that creates new data simply store the data in GUIDATA like you show, then in each callback that uses this data you will have to retrieve it by calling GUIDATA again.
For example, in your CALCULATE function, you store a structure called handles in GUIDATA. If you want to access this same structure in a different function later, you will need to do:
H = guidata(gcf);
Then H will have that same structure. The automatic structure handles just stores the handles to the figure and uicontrol elements. It doesn't get updated automatically.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!