Clear Filters
Clear Filters

GUI loses state when destroyed and recreated

4 views (last 30 days)
Kurt on 26 Oct 2022
Edited: Kurt on 1 Nov 2022
This USED to work several days ago, but somehow I broke it.
I have a GUI which I created in App Designer. The GUI is called from a Matlab function via a button push. The GUI has its own "Exit" button, or I can just click the X in the upper right corner. Until recently, the state of the GUI was preserved across creations and deletions, but now it "forgets" its state when destroyed, i.e., radio buttons and other components revert to their initial state. What did I break?
I deleted the whole GUI and re-created it in App Designer with just three radio buttons, and that version also loses state. Here is the code:
In my main menu GUI:
function SearchOptionsButtonPushed(~,~) % this is the button to launch the GUI
Here is the SearchOptions class:
classdef SearchOptions < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
MountTypePanel matlab.ui.container.Panel
ButtonGroup matlab.ui.container.ButtonGroup
RADECButton matlab.ui.control.RadioButton
AZELButton matlab.ui.control.RadioButton
OffButton matlab.ui.control.RadioButton
% Callbacks that handle component events
methods (Access = private)
% Selection changed function: ButtonGroup
function ButtonGroupSelectionChanged(app, event)
global AzEl_mode;
selectedButton = app.ButtonGroup.SelectedObject;
AzEl_mode = get(event.NewValue, 'Tag' );
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create MountTypePanel
app.MountTypePanel = uipanel(app.UIFigure);
app.MountTypePanel.Title = 'Mount Type';
app.MountTypePanel.Position = [52 359 260 103];
% Create ButtonGroup
app.ButtonGroup = uibuttongroup(app.MountTypePanel);
app.ButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ButtonGroupSelectionChanged, true);
app.ButtonGroup.Position = [21 23 221 34];
% Create OffButton
app.OffButton = uiradiobutton(app.ButtonGroup);
app.OffButton.Tag = '0';
app.OffButton.Text = 'Off';
app.OffButton.Position = [17 6 42 22];
app.OffButton.Value = true;
% Create AZELButton
app.AZELButton = uiradiobutton(app.ButtonGroup);
app.AZELButton.Tag = '1';
app.AZELButton.Text = 'AZ/EL';
app.AZELButton.Position = [70 6 46 22];
% Create RADECButton
app.RADECButton = uiradiobutton(app.ButtonGroup);
app.RADECButton.Tag = '2';
app.RADECButton.Text = 'RA/DEC';
app.RADECButton.Position = [136 6 59 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
% App creation and deletion
methods (Access = public)
% Construct app
function app = SearchOptions
% Create UIFigure and components
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
All very basic, just three radio buttons, but when I destroy the GUI, the state is lost. The AzElMode global is used elsewhere in my app.
How do I preserve the state? I found a very old discussion on this at the following link, but it's 12 years old:

Answers (1)

Kurt on 27 Oct 2022
Edited: Kurt on 1 Nov 2022
Found the solution. Change the GUI callback function to read as follows:
function GUIButtonGroupSelectionChanged(app, event) % or whatever your GUI is named
global AzEl_mode;
AzEl_mode = get(event.NewValue, 'Tag' );
Then in the parent class where the component is initialized, add the following function:
function GUIButtonPushed(~,~)
GUIPanel = GUI; % instantiate the GUI
global AzEl_mode;
if (AzEl_mode == 1)
GUIPanel.AZELButton.Value = true; % your button names may vary
elseif (AzEl_mode == 2)
GUIPanel.AZELButton.Value = true;
Note that there is no need to include the "AzEL_mode == 0" case, because the GUI already defaults to 0 when it is initialized.
Pulldown menus can be handled the same way, except the Value is set to one of the drop-down item values, as follows:
global scan_mode_index
if (scan_mode_index == 1)
GUIPanel.Dropdown.Value = 'Notary Sojak'
where the global scan_mode_index was saved in the callback function.
There may be better methods that don't require the use of globals, but this works for me.


Find more on Develop Apps Using App Designer 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!