Saving and loading variables to matfile in appdesigner
8 views (last 30 days)
Show older comments
Przemyslaw Piechota
on 23 Dec 2019
Commented: Adam Danz
on 2 Jan 2020
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
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
Adam Danz
on 23 Dec 2019
It appears that "GlodSlider" is a UI object that you're saving to a mat file. The better approach is to save the Value of that object rather than the object itself. Then you can load the value at startup (named GlodSliderValue below).
When the GUI is first used, there may not be a mat file to load so you may want to check that the file exists before loading in the values.
Lastly, it's best to define where to look for the file by using a full path to the file rather than just the file name.
% Code that executes after component creation
function startupFcn(app)
datafile = fullfile('C:\Users\name\Documents\MATLAB','danegry.mat');
if exist(datafile,'file') == 2
app.data = load(datafile);
app.GlodSlider.Value = app.data.GlodSliderValue;
end
end
4 Comments
Adam Danz
on 2 Jan 2020
The error message tells you what the problme is: "'Value' must be a double scalar within the range of 'Limits'."
You either need to increase the limits or ensure that you're not setting the value greater than the upper limit.
More Answers (1)
See Also
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!