Manually programmed GUI, guidata() not updating the handles struct

1 view (last 30 days)
The basic structure of my GUI is a figure with multiple tabs. Each tab is a panel with its own children buttons and other uicontrols. In one of the panels, i have a PushButton with a callback to a function that counts the amount of checkboxes selected on a different panel. My main handle structure is called 'TabHandles', where i store the overall figure object, each panel object, and other things. The pushbutton is coded as follows:
numberEventsButton = uicontrol('Parent',TabHandles(NumberOfTabs).TabPanel,'Style','PushButton',...
'String','# of Events Currently Selected (Push)','Callback',{@currentSelectionCounter});
The callback function is:
function currentSelectionCounter(~,~)
TabHandles = guidata(gcf);
totEvents = TabHandles(1).Counter;
TabHandles(2).Counter = 0;
for k=1:totEvents
newHandles(2).Counter = newHandles(2).Counter + get(newHandles(k).EventCheckbox,'Value');
end
guidata(gcf,TabHandles)
end
Using the debugger, the new local version of 'TabHandles' created in the callback function does indeed have the correct new value I'd like to pass to the main GUI handles sturct (which is TabHandles(2).Counter) , but after using guidata(gcf,TabHandles) the handles structure in the main GUI workspace does not update. I use guidata(gcf) to get the lastest copy of the TabHandles struct, and i then use guidata() with the 2 inputs to, in theory, save my new modified TabHandles back to the maimn figure. What am I doing wrong? Is there an issue with the PushButton being a child component of a panel, which is itself a child component of the figure? Been scratching my head for a while now
  2 Comments
Rik
Rik on 16 Aug 2022
Did you reload the struct from guidata after editing it in this subfunction?
Benjamin Sorel
Benjamin Sorel on 16 Aug 2022
I think that's my issue...im not sure how or where to do that. After looking throught the debugger more, I found that the handles struct in fact WAS updating in real time, but when I would cancel the gui and look in the workspace, the struct wouldnt be updated.
How do I reload the struct after editing it?

Sign in to comment.

Accepted Answer

Rik
Rik on 16 Aug 2022
The guidata struct is not a global variable. You should treat it as a mat file. When you write to it, other functions need to reload it to apply the changes.
So at any point where you expect changes to occur, make sure you reload the guidata struct.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!