How to save all state in my App Designer app?

40 visualizaciones (últimos 30 días)
Ken
Ken el 18 de Abr. de 2024
Editada: Nils Wiesendahl el 14 de Nov. de 2024 a las 13:42
I have built an app for a specialized scientific analysis using App Designer. It is a complex interface with 8 tabs, and well over 100 controls. And it will grow.
It would be very helpful if I could provide my users with a "Save all settings" button. Question: is there a good way to programmatically enumerate all Components (as distinct from other properties or methods) in the app? And then, to read or set the value for each?

Respuestas (2)

Ken
Ken el 21 de Abr. de 2024
Movida: Voss el 21 de Abr. de 2024
That example was half the answer, but it required enumerating the components to be saved, which doesn't grow well. I wanted to automatically traverse the app data structure and FIND programmatically all components whose values should be saved. Here's what worked for me. This solution will restore all components that match, leave alone all components that are not found in the saved data, and warn (in the Matlab window) of any saved state that did not match a component in the program.
This creates a robust save/load mechanism that will not crash even in the presence of version changes that add or delete components.
function SaveSettings(app)
state = struct();
props = properties(app);
for i = 1:length(props)
name = props{i};
comp = app.(name); % May be a component or a property, so far...
c = string(class(comp));
if c == "matlab.ui.control.NumericEditField" ...
|| c == "matlab.ui.control.EditField" ...
|| c == "matlab.ui.control.CheckBox"
state.(name)= comp.Value;
elseif c == "matlab.ui.control.DropDown"
% These are weird, had to be specially handled to save a string
% instead of an index into the list:
v = comp.Value;
state.(name)= comp.Items{v};
end
end
save('SavedSettings.mat','state');
end
function LoadSettings(app)
load('SavedSettings.mat', 'state')
fields = fieldnames(state);
for i = 1:length(fields)
name = fields{i};
try % or could use isprop(app, name) to test presence
comp = app.(name);
comp.Value = state.(name);
catch err
disp(['Did not use saved field', name, ': ', err.identifier])
end
end
end
  1 comentario
Nils Wiesendahl
Nils Wiesendahl el 14 de Nov. de 2024 a las 13:39
Editada: Nils Wiesendahl el 14 de Nov. de 2024 a las 13:42
Slight Addition to Ken's Version. I have added CheckBoxTrees, ListBox and UITables as well now!
function SaveAppStateButtonPushed(app)
state = struct();
props = properties(app);
for i = 1:length(props)
name = props{i};
comp = app.(name); % May be a component or a property, so far...
c = string(class(comp));
if c == "matlab.ui.control.NumericEditField" ...
|| c == "matlab.ui.control.EditField" ...
|| c == "matlab.ui.control.CheckBox"
state.(name)= comp.Value;
elseif c == "matlab.ui.control.DropDown"
% These are weird, had to be specially handled to save a string
% instead of an index into the list:
v = comp.Value;
if ~isempty(v)
id = find(strcmp(comp.Items,v));
state.(name)= comp.Items{id};
end
elseif c == "matlab.ui.container.CheckBoxTree"
for ii = 1:size(comp.CheckedNodes,1)
tag(ii) = string(app.(name).CheckedNodes(ii).Tag);
end
if exist('tag','var')
state.(name) = tag;
end
elseif c == "matlab.ui.control.ListBox"
state.(name) = comp.Items;
elseif c == "matlab.ui.control.Table"
state.(name) = comp.Data;
end
end
save('SavedSettings.mat','state');
end
function LoadAppStateButtonPushed(app)
load('SavedSettings.mat', 'state')
fields = fieldnames(state);
for i = 1:length(fields)
name = fields{i};
try % or could use isprop(app, name) to test presence
comp = app.(name);
if class(comp) == "matlab.ui.container.CheckBoxTree"
tags = state.(name);
for ii = 1:size(tags,2)
checkedNodes(ii) = findall(0,'tag',tags(ii));
end
app.(name).CheckedNodes = checkedNodes;
elseif class(comp) == "matlab.ui.control.ListBox"
comp.Items = state.(name);
elseif class(comp) == "matlab.ui.control.Table"
comp.Data = state.(name)
else
comp.Value = state.(name);
end
catch err
disp(['Did not use saved field', name, ': ', err.identifier])
end
end
end

Iniciar sesión para comentar.


Taylor
Taylor el 18 de Abr. de 2024

Categorías

Más información sobre Dialog Boxes en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by