How to save and load MATLAB app?

14 ビュー (過去 30 日間)
William
William 約9時間 前
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

回答 (1 件)

Matt J
Matt J 約7時間 前
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.
  1 件のコメント
Image Analyst
Image Analyst 約5時間 前
I think he is when he does this:
app.(propName) = config.(propName);
The assumption being that config has fields that match the properties of the app.
However I don't know that doing that (saving config) will save all the settings of the various widgets on the screen, like scrollbar values, radio button states, edit field contents, etc. I'd have to check on that. Personally I avoid dynamic field names so I'd just use the known field names. Use them by their known name. There is probably not more than 20 settings to save so it shouldn't be too onerous to save and recall them by name.

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

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by