How to save and load MATLAB app?
23 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
Answers (2)
Matt J
on 27 Nov 2024
The app object contains handles to all the app's uicontrols, but your LoadConfigMenuSelected callback doesn't reference any of them. Surely you need to set their properties in some way as well if the load operation is supposed to have some graphical effect.
3 Comments
Image Analyst
on 27 Nov 2024
@William, see the official Mathworks support on the topic of saving the state or values of controls from one of your app's sessions to the next:
0 Comments
See Also
Categories
Find more on Develop uifigure-Based Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!