Not valid variable name despite variable declaration i MATLAB app designer.

35 views (last 30 days)
After the analysis of the images, I want to save the parameters in .mat file. The following code is for the same,
function SaveButtonPushed(app, event)
app.filename = strcat(app.filename,'.mat');
if isempty(app.FLG)
save(app.filename,'app.para_BF_new','app.ROI')
elseif isempty(app.FLR)
save(app.filename,'app.para_BF_new','app.para_FLG','app.ROI')
elseif isempty(app.FLFR)
save(app.filename,'app.para_BF_new','app.para_FLG','app.para_FLR','app.ROI')
else
save(app.filename,'app.para_BF_new','app.para_FLG','app.para_FLR','app.para_FLFR','app.ROI')
end
However, the following error is coming in MATLAB app designer but is working fine as simple Matlab code. I have already declared all the variables properly.
Error using save
'app.para_BF_new' is not a valid variable name.
Error in SpehroidAnalysisApp/SaveButtonPushed (line 181)
save(app.filename,'app.para_BF_new','app.ROI')
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, ...
Error while evaluating Button PrivateButtonPushedFcn.

Accepted Answer

Steven Lord
Steven Lord on 13 Mar 2023
'app.para_BF_new' is not a valid variable name.
The error message is correct. 'app.para_BF_new' is not a valid variable name. It is an expression that refers to a property of your app. The save function requires you to specify variable names not expressions when indicating which variables to save.
In this case what I'd likely do is to create a struct array with the appropriate fields and then call save with the '-struct' option. Since I don't have your app I've changed the if statements so I can run the example in MATLAB Answers.
app.filename = fullfile(tempdir, 'myfile'); % Added to make this example run
app.filename = strcat(app.filename,'.mat');
You probably don't want to unconditionally add '.mat' to the end of the filename property. If you run this code more than once, do you want 'myfile' to become first 'myfile.mat' then 'myfile.mat.mat'? Consider using the fileparts function to break the filename into path information, file name information, and file extension. Then you could reassemble the filename using fullfile, adding in an extension if fileparts indicated it didn't have one originally.
toBeSaved = struct;
if 1+1 == 2 % true, isempty(app.FLG)
toBeSaved.ROI = 42;
end
if 2+2 == 4 % true, isempty(app.FLR)
toBeSaved.para_FLG = 999;
end
if 3+3 == 5 % false, isempty(app.FLFR)
toBeSaved.para_FLR = -1;
end
if 4+4 == 8 % true, else
toBeSaved.para_FLFR = 'Hello World!';
end
save(app.filename, '-struct', 'toBeSaved')
Let's check what is in the file, first using whos to list the variables in the file then calling load to load them back into a struct.
whos('-file', app.filename)
Name Size Bytes Class Attributes ROI 1x1 8 double para_FLFR 1x12 24 char para_FLG 1x1 8 double
data = load(app.filename)
data = struct with fields:
ROI: 42 para_FLG: 999 para_FLFR: 'Hello World!'
The body of the third if statement was not executed, so toBeSaved does not contain a field para_FLR and so neither does the MAT-file. If I wanted to update the properties of app when I load this data I could do so using dynamic property names. This syntax updates fields in the app struct but the same approach works for properties like those of an app object.
app
app = struct with fields:
filename: '/tmp/myfile.mat'
fieldsInStruct = fieldnames(data);
for whichField = 1:numel(fieldsInStruct)
name = fieldsInStruct{whichField};
app.(name) = data.(name);
end
app
app = struct with fields:
filename: '/tmp/myfile.mat' ROI: 42 para_FLG: 999 para_FLFR: 'Hello World!'
  4 Comments
Steven Lord
Steven Lord on 13 Mar 2023
Something along the lines of this should be close to what you're trying to do. Note that this code is not tested.
function SaveButtonPushed(app, event)
app.filename = strcat(app.filename,'.mat');
% app.para_BF_new is always saved so start the struct with that property
toBeSaved = struct('para_BF_new', app.para_BF_new);
if isempty(app.FLG)
toBeSaved.ROI = app.ROI;
end
if isempty(app.FLR)
toBeSaved.para_FLG = app.para_FLG;
end
if isempty(app.FLFR)
toBeSaved.para_FLR = app.para_FLR;
end
toBeSaved.para_FLFR = app.para_FLFR;
save(app.filename, '-struct', 'toBeSaved')
Amlan basu
Amlan basu on 17 Mar 2023
Hi Steven,
Please can you guide me on how to save the file (using the above code) by taking the path and file name input from the user.
I know uiputfile is the best option for this. But since I am novice to app designer, I am unable to implement the same.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!