Borrar filtros
Borrar filtros

GUI loses state when destroyed and recreated

4 visualizaciones (últimos 30 días)
Kurt
Kurt el 26 de Oct. de 2022
Editada: Kurt el 1 de Nov. de 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
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:

Respuestas (1)

Kurt
Kurt el 27 de Oct. de 2022
Editada: Kurt el 1 de Nov. de 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' );
end
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;
end
end
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.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by