GUI loses state when destroyed and recreated
Show older comments
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
SearchOptions;
end
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
end
% Callbacks that handle component events
methods (Access = private)
% Selection changed function: ButtonGroup
function ButtonGroupSelectionChanged(app, event)
global AzEl_mode;
selectedButton = app.ButtonGroup.SelectedObject;
%disp(selectedButton);
AzEl_mode = get(event.NewValue, 'Tag' );
disp(AzEl_mode);
end
end
% 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';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = SearchOptions
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
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)
Categories
Find more on Develop uifigure-Based Apps 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!