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

13 ビュー (過去 30 日間)
Ron
Ron 2022 年 9 月 15 日
コメント済み: Ron 2022 年 9 月 19 日
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')
Ron
Ron 2022 年 9 月 19 日
Well that's work perfect!
Thank you very much.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by