Not valid variable name despite variable declaration i MATLAB app designer.
35 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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)
data = load(app.filename)
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
fieldsInStruct = fieldnames(data);
for whichField = 1:numel(fieldsInStruct)
name = fieldsInStruct{whichField};
app.(name) = data.(name);
end
app
4 Comments
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')
More Answers (0)
See Also
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!