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

조회 수: 14(최근 30일)
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)
elseif isempty(app.FLR)
elseif isempty(app.FLFR)
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)
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.

채택된 답변

Steven Lord
Steven Lord 2023년 3월 13일
'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;
if 2+2 == 4 % true, isempty(app.FLR)
toBeSaved.para_FLG = 999;
if 3+3 == 5 % false, isempty(app.FLFR)
toBeSaved.para_FLR = -1;
if 4+4 == 8 % true, else
toBeSaved.para_FLFR = 'Hello World!';
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 = struct with fields:
filename: '/tmp/myfile.mat'
fieldsInStruct = fieldnames(data);
for whichField = 1:numel(fieldsInStruct)
name = fieldsInStruct{whichField};
app.(name) = data.(name);
app = struct with fields:
filename: '/tmp/myfile.mat' ROI: 42 para_FLG: 999 para_FLFR: 'Hello World!'
  댓글 수: 4
Amlan basu
Amlan basu 2023년 3월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by