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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by