How to save and load all the states of checkboxes and etc

13 vues (au cours des 30 derniers jours)
Ron
Ron le 15 Sep 2022
Commenté : Ron le 19 Sep 2022
Helllo ,
I'm trying to save and load usin the save() and load() functions , the second argument is the "variables" , if I want to save 30 variables it means i have to staticly write all of them in save and load functions , is there any other way to save ALL of the variables and load all of them? i mean i want to make few configurations by names , firstConfiguration.mat , secondConfiguration.mat , and when i load them , instantly it loads the checkboxes that there is in the configuration
  4 commentaires
Konrad
Konrad le 15 Sep 2022
Hi Ron,
are you using the App Designer to create a GUI? And you want to load the state of your GUI components (like checkboxes) from a file?
Ron
Ron le 15 Sep 2022
Yes, I want to save all the states for a file , and be able to load it (but i want all of them and not some specific).
Obviously when i say save and load I mean , for the same GUI and not mixing 2 different gui's

Connectez-vous pour commenter.

Réponse acceptée

Konrad
Konrad le 15 Sep 2022
Hi Ron,
here is something I recently used in an App Designer GUI:
The App needs a property 'config' and the following method to store the UI-state in the 'config' property:
function storeUIpropConfig(app, source, property, value)
% find object name
p = properties(app);
k = cellfun(@(x)isequal(app.(x),source),p);
if sum(k)~=1
warning('Could not store UIproperty in app config. Callback source not found in app properties!');
return;
end
app.config.UIproperties.(p{k}).(property) = value;
end
Let's assume the figure handle is stored in the app property 'UIFigure'. Then you can find all checkboxes and edifields with
h = findobj(app.UIFigure,'Type','uieditfield','-or',...
'Type','uinumericeditfield','-or',...
'Type','uicheckbox');
and use the method above to store the 'Value' property of the ui objects in the config property:
for k = 1:numel(h)
app.storeUIpropConfig(h(k),'Value',h(k).Value);
end
You can also use the ValueChangedFcn callback of the ui-objects to call that method each time the 'Value' changes.
Then save the config to disk:
cfg = app.config.UIproperties;
save('config.mat','-struct','cfg');
To load the config use:
app.config.UIproperties = load('config.mat');
and then run the following method to set the state of the UI objects:
function restoreUIpropConfig(app)
obj = fieldnames(app.config.UIproperties);
for io = 1:numel(obj)
o = obj{io};
props = fieldnames(app.config.UIproperties.(o));
for ip = 1:numel(props)
p = props{ip};
try
app.(o).(p) = app.config.UIproperties.(o).(p);
catch ME
app.config.UIproperties.(o) = rmfield(app.config.UIproperties.(o),p);
warning('Failed to restore property value for %s.%s because:\n%s',o,p,ME.message);
end
end
end
end
Let me know if you need more help.
Best, Konrad
  5 commentaires
Konrad
Konrad le 19 Sep 2022
Modifié(e) : Konrad le 19 Sep 2022
You can also simply find all objects in your app that have a 'Value' property and store/restore these:
h = findobj(app.UIFigure,'-property','Value')
Ron
Ron le 19 Sep 2022
Well that's work perfect!
Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by