Saving and Loading MATLAB APP Configuration
11 views (last 30 days)
Show older comments
Hello,
I have a MATLAB App that has been devolped in the MATLAB App Designer. I have saved all of the apps properties when the user selects the save config menu option and saved it into a .mat file.
Now I want to be able to load this file and update the app to the properties saved in this file. Currently, the program will store the variables in the app structure, but will not update the app's UIFigure. If I save the the UIFigure itself the program will create a new UIFigure and the code in App Designer will no longer work. Hence, why I've added not to save the UIFigure.
if strcmp(propName, 'SuperMarioUIFigure')
continue;
end
Does anyone know how I can get the app structure to update the current UIFigure that App Designer is running?
Here is my current code.
% Menu selected function: SaveConfigMenu
function SaveConfigMenuSelected(app, event)
% Create a structure to hold all the properties of the app
config = struct();
% List all properties of the app
metaProps = properties(app);
% Loop through properties and store their values in structure
for i = 1:length(metaProps)
propName = metaProps{i};
if strcmp(propName, 'SuperMarioUIFigure')
continue;
end
try
config.(propName) = app.(propName);
catch
% Handle any properties that can't be saved
fprintf('Could not save property: %s\n', propName);
end
end
% Prompt user to specify a file to save the configuration
[file, path] = uiputfile('*.mat', 'Save Configuration As');
if isequal(file, 0) || isequal(path, 0)
return; % User cancelled
end
% Save the configuration structure to a file
save(fullfile(path, file), '-struct', 'config');
uialert(app.SuperMarioUIFigure, 'Configuration saved successfully!', 'Save Configuration');
end
% Menu selected function: LoadConfigMenu
function LoadConfigMenuSelected(app, event)
% Prompt user to select a configuration file
[file, path] = uigetfile('*.mat', 'Load Configuration');
if isequal(file, 0) || isequal(path, 0)
return; % User cancelled
end
% Load the configuration structure from the file
config = load(fullfile(path, file));
% List all properties of the app
metaProps = properties(app);
% Update app properties with values from the configuration file
for i = 1:length(metaProps)
propName = metaProps{i};
if isfield(config, propName)
try
app.(propName) = config.(propName);
catch ME
% Handle any properties that can't be updated
fprintf('Could not load property: %s. Error: %s\n', propName, ME.message);
end
end
end
% Display a success message
uialert(app.SuperMarioUIFigure, 'Configuration loaded successfully!', 'Load Configuration');
end
Thanks
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!