Saving and loading variables to matfile in appdesigner

8 views (last 30 days)
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
Adam Danz on 23 Dec 2019
What do you mean "public variable"?
What is app.data?
Adam Danz
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

Sign in to comment.

Accepted Answer

Adam Danz
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
Przemyslaw Piechota
Przemyslaw Piechota on 1 Jan 2020
Edited: Przemyslaw Piechota on 1 Jan 2020
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"?
Adam Danz
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.

Sign in to comment.

More Answers (1)

Przemyslaw Piechota
Przemyslaw Piechota on 23 Dec 2019
Edited: Przemyslaw Piechota on 23 Dec 2019
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

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!