How to save and load all the states of checkboxes and etc
13 次查看(过去 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
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?
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!