How to save and load App Designer app state in between sessions

44 vues (au cours des 30 derniers jours)
Israel Solha
Israel Solha le 17 Juin 2019
Commenté : Gabriel Arthur le 20 Juil 2020
I've seen similar questions about how to save/load variables from an app, but I couldn't find a way to save the state of my app (including all variables, all states of edittexts, etc) when the app is closed, and loading it when it's started. I tried the following code, with no luck. I thought that by just giving a name to the file it would by default save everything, but when I try to access the file just saved I get the app with the same structure as the previous, but all properties have: The variable app.something does not exist. Also, how can i clear all properties values and set everything to the state the app would be if no file was loaded?
function startupFcn(app)
try
load('tccworkspace.mat');
catch
return;
end
end
function ElementosUIFigureCloseRequest(app, event)
delete(app);
save('tccworkspace.mat');
end
  2 commentaires
Guillaume
Guillaume le 18 Juin 2019
but all properties have: The variable app.something does not exist
Well, yes, before saving your mat file you delete the app object, in effect making the app handle invalid. I suspect it would work a bit better if you delete the app after you save it to a file.
I can't comment on the rest of your question as I don't know enough about the App designer.
Israel Solha
Israel Solha le 18 Juin 2019
I noticed that mistake shortly after posting. Unfortunately, changing the order didn’t help, as I’ve explained in my reply to the other answer.

Connectez-vous pour commenter.

Réponses (1)

Dennis
Dennis le 18 Juin 2019
You should load your .mat file into a structure, and pass that structure to other functions.
S=load('tccworkspace.mat');
You can set S to a property of your app to access it in other functions.
  5 commentaires
hubert taieb
hubert taieb le 17 Août 2019
Hello !
I am having the same problem, so far I solved it by using the setfield method, manually for all of the property of the element of the app. It is not so clean but it does the job...
If there is a cleaner version I would be interested !
Thanks
Gabriel Arthur
Gabriel Arthur le 20 Juil 2020
I wrestled with this issue for a while as well, I think I've got a work-around.
I only cared about the value and visibility of each app property, so that what I save and load.
function SaveButtonPushed(app, event)
props = properties(app);
values = cell(1, length(props));
visibilities = cell(1, length(props));
for i = 1:length(props)
propName = props{i};
property = app.(propName);
if isprop(property, 'Value')
values{i} = app.(propName).Value;
end
if isprop(property, 'Visible')
visibilities{i} = app.(props{i}).Visible;
end
end
file = uiputfile('*.mat', "Save Message" );
if file
save(file, 'props', 'values', 'visibilities');
end
end
function LoadButtonPushed(app, event)
file = uigetfile('*.mat', "Load Message");
if file
load(file, 'props', 'values', 'visibilities');
end
for i = 1:length(props)
propName = props{i};
property = app.(propName);
if isprop(property, 'Value')
app.(propName).Value = values{i};
end
if isprop(property, 'Visible')
app.(props{i}).Visible = visibilities{i};
end
end
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Create Custom UI Components dans Help Center et File Exchange

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by