Saving and loading variables to matfile in appdesigner
Show older comments
I'm doing a game in appdesigner similar to tamagotchi. The issue is that i want to save current status of character (some variables) in a matfile while executing CloseRequestFcn and loading them again in StartupFcn. I've looked on similar topics but not a single one solution worked. I'm getting the error:
Reference to non-existent field 'GlodSlider'.
Error in tamagotchi (line 355)
runStartupFcn(app, @startupFcn)
I'm attaching the danegry.mat file.
properties (Access = public)
data; % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.data = load('danegry.mat');
app.GlodSlider.Value = app.data.GlodSlider.Value;
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
save('danegry.mat','app');
delete(app)
end
4 Comments
Adam Danz
on 23 Dec 2019
"Reference to non-existent field 'GlodSlider'."
Apparently GlodSlider is not a field in App -or- GlodSlider is not a field in data. .
% Code that executes after component creation
function startupFcn(app)
app.data = load('danegry.mat');
app.GlodSlider.Value = app.data.GlodSlider.Value; % <----- HERE
end
First check for misspellings or problems with upper/lower case.
Where does 'data' come from? Is it something that's added to App after the app starts running? If so, 'data' won't exist during startup.
Adam Danz
on 23 Dec 2019
Data is the public variable to which I assign the loaded data from matfile. When I open the app matfile in matlab it is shown that field GlodSlider exists.
Adam Danz
on 23 Dec 2019
What do you mean "public variable"?
What is app.data?
Adam Danz
on 23 Dec 2019
Przemyslaw Piechota's answer moved here as a comment
data is a variable defined in properties(I was modyfing the code on my own while waiting for answer)
app.data is call for this variable to operate on it. I'm new to appdesigner so I just followed the internet and compiler tips.
I just want to save the Value's written to data array in CloseRequestFcn to matfile when closing app. When opening it again I want to load those value's and overwrite the default ones in StartupFcn.
EDIT: Those value's are numeric.
properties (Access = private)
val1; % Zmienna pomocnicza Głodu
val2; % Zmienna pomocnicza Snu
val3; % Zmienna pomocnicza Zabawy
val4; % Zmienna pomocnicza Zdrowia
val5; % Druga Zmienna pomocnicza Zdrowia
count1; % Zmienna ilości
count2; % Zmienna ilości 2
data; % I'm loading the matfile into this variable
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
app.data = [app.GlodSlider.Value app.SenSlider.Value app.ZdrowieSlider.Value app.EditField_Jedzenie app.EditField_Zdrowie];
end
Accepted Answer
More Answers (1)
Przemyslaw Piechota
on 23 Dec 2019
Edited: Przemyslaw Piechota
on 23 Dec 2019
1 Comment
Adam Danz
on 23 Dec 2019
Please copy this as a comment under my answer and then I'll respond.
Categories
Find more on Develop Apps Using App Designer 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!