Saving and loading variables to matfile in appdesigner

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

"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.
Przemyslaw Piechota's answer moved here as a comment.
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.
What do you mean "public variable"?
What is app.data?
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

Sign in to comment.

 Accepted Answer

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

@ Przemyslaw Piechota, please reserve the answer section for answers/solutions and use the comment section for discussion.
Przemyslaw Piechota's answer moved here as a comment.
Okey, but how I should save the data?
[UPDATED comment]
Your current UIFigureCloseRequest function should be saving the Value of the slider, not the slider handle itself and especially not the app class handle. Also, add the fullfile method I demonstrated in my answer, too.
function UIFigureCloseRequest(app, event)
GlodSliderValue = app.GlodSlider.Value;
datafile = fullfile('C:\Users\name\Documents\MATLAB','danegry.mat');
save(datafile,'GlodSliderValue');
delete(app)
end
To continue the discussion, please leave a comment direction below this one.
Problem solved. Thank you for help. There is the complete code.
Solution for previous problem
properties (Access = public)
data;
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
datafile = fullfile('C:\Users\hator\OneDrive\Pulpit\projekt matlab','danegry.mat');
if exist(datafile,'file') == 2
app.data = load(datafile);
app.GlodSlider.Value = app.data.GlodSliderValue;
app.SenSlider.Value = app.data.SenSliderValue;
app.ZdrowieSlider.Value = app.data.ZdrowieSliderValue;
app.EditField_Jedzenie.Value = app.data.EditField_JedzenieValue;
app.EditField_Zdrowie.Value = app.data.EditField_ZdrowieValue;
end
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
GlodSliderValue = app.GlodSlider.Value;
SenSliderValue = app.SenSlider.Value;
ZdrowieSliderValue = app.ZdrowieSlider.Value;
EditField_JedzenieValue = app.EditField_Jedzenie.Value;
EditField_ZdrowieValue = app.EditField_Zdrowie.Value;
datafile = fullfile('C:\Users\hator\OneDrive\Pulpit\projekt matlab','danegry.mat');
save(datafile,'GlodSliderValue','SenSliderValue','ZdrowieSliderValue','EditField_JedzenieValue','EditField_ZdrowieValue');
delete(app)
end
I have a different question now. If i have a slider with limits [0 100] and I'm exceeding limits with example line:
variable = variable + 40;
Why does app designer crash with:
Error using matlab.ui.control.internal.model.AbstractInteractiveTickComponent/set.Value (line 95)
'Value' must be a double scalar within the range of 'Limits'.
Code for the Limits :
function startupFcn(app)
app.GlodSlider.Limits = [0 100];
app.SenSlider.Limits = [0 100];
app.ZdrowieSlider.Limits = [0 100];
app.EditField_Jedzenie.Limits = [0 100];
app.EditField_Zdrowie. Limits = [0 100];
datafile = fullfile('C:\Users\hator\OneDrive\Pulpit\projekt matlab','danegry.mat');
if exist(datafile,'file') == 2
app.data = load(datafile);
app.GlodSlider.Value = app.data.GlodSliderValue;
app.SenSlider.Value = app.data.SenSliderValue;
app.ZdrowieSlider.Value = app.data.ZdrowieSliderValue;
app.EditField_Jedzenie.Value = app.data.EditField_JedzenieValue;
app.EditField_Zdrowie.Value = app.data.EditField_ZdrowieValue;
end
Code for exceeding limits:
% Image clicked function: Im_Zdrowie
function Im_ZdrowieImageClicked(app, event)
% Efekty Zdrowia
if app.ZdrowieSlider.Value < 100 && app.GlodSlider.Value > 0 && app.EditField_Zdrowie.Value > 0
app.ZdrowieSlider.Value = app.ZdrowieSlider.Value + 40; % +40 Zdrowia
app.EditField_Zdrowie.Value = app.EditField_Zdrowie.Value - 1;
app.GlodSlider.Value = app.GlodSlider.Value - 20; % -20 Głodu
%/////////////////////////////////////////////////
% Wyjątek kiedy Głód = 0
elseif app.ZdrowieSlider.Value < 100 && app.GlodSlider.Value == 0 && app.EditField_Zdrowie.Value > 0
app.ZdrowieSlider.Value = app.ZdrowieSlider.Value - 10; % -10 Zdrowia
app.EditField_Zdrowie.Value = app.EditField_Zdrowie.Value - 1;
end
%/////////////////////////////////////////////////
end
Should I define Limits within the function of "Image clicked function"?
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.

Sign in to comment.

More Answers (1)

The photo show opening danegry.mat in matlab.
Bez tytułu.png
I'm getting this while loading file. I've saved the new content first.
Reference to non-existent field 'GlodSlider'.
Error in tamagotchi (line 357)
runStartupFcn(app, @startupFcn)
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)
datafile = fullfile('C:\Users\hator\OneDrive\Pulpit\projekt matlab','danegry.mat');
if exist(datafile,'file') == 2
app.data = load(datafile);
app.GlodSlider.Value = app.data.GlodSlider.Value;
end
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
datafile = fullfile('C:\Users\hator\OneDrive\Pulpit\projekt matlab','danegry.mat');
if exist(datafile,'file') == 2
save('danegry.mat','app');
end
delete(app);
end

1 Comment

Please copy this as a comment under my answer and then I'll respond.

Sign in to comment.

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!