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

7 次查看(过去 30 天)
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 个评论
Konrad
Konrad 2022-9-15
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 2022-9-15
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

请先登录,再进行评论。

采纳的回答

Konrad
Konrad 2022-9-15
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 个评论
Konrad
Konrad 2022-9-19
编辑:Konrad 2022-9-19
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')

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by