Why do I get unrecognized function or variable when calling constructor in app designer

3 次查看(过去 30 天)
I want to call the constructor from another class in my app's properties like this:
properties (Access = private)
oBenignDataset = ImageData('\\10.1.1.70\Projekte\Projekt_2223\pr5ahbg121232\Data', 'benign');
oMalignantDataset = ImageData('\\10.1.1.70\Projekte\Projekt_2223\pr5ahbg121232\Data', 'malignant');
analyzer = FeatureAnalyzer(TFeature);
end
The error occures at "analyzer = FeatureAnalyzer(TFeature);". It works for the other classes' constructor (ImageData) but not for this one. I get the error:
Invalid default value for property 'analyzer' in class 'GUI_FINAL':
Unrecognized function or variable 'TFeature'.
The constructor in my class looks like this:
function obj = FeatureAnalyzer(TFeature)
obj.TFeature = TFeature;
end
How can I fix this error?
  2 个评论
Chris
Chris 2023-1-27
编辑:Chris 2023-1-27
Can you invoke a TFeature from the command window while located in the app's parent directory, or do you get a similar error?
test = TFeature;
You should be able to do this successfully with ImageData:
test = ImageData('\\10.1.1.70\Projekte\Projekt_2223\pr5ahbg121232\Data', 'benign');
If the app needs to generate a variable called TFeature to use, then I don't think it can be done by setting a property value default. The property would have to be set in the app's StartupFcn.
Kevin Gjoni
Kevin Gjoni 2023-1-28
it works fine in the command window. It gives me a filled table. TFeature is a variable in the workspace, so it doesn't work when I clear my workspace.
Setting the property in the app's StartupFcn gave me the same error.

请先登录,再进行评论。

采纳的回答

Kevin Gjoni
Kevin Gjoni 2023-1-28
I solved the problem:
The TFeature that i passed to the constructer is a table in the workspace. I don't know why but I could access it, so I tried to add it to the path. I did this by converting the table to an excel file which then is added to my path:
>> TFeature = evalin('base', 'TFeature');
>> writetable(TFeature,'TFeature.xlsx','FileType','spreadsheet')
To access the table in my app i used the readtable function (in my StartupFcn):
TFeature = readtable('TFeature.xlsx');
analyzer = FeatureAnalyzer(TFeature);
Then I was able to call the constructer without errors.

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-1-28
TFeature is not a property or method of the class, and it does not exist at the time the properties list is being parsed. It is local to your constructor.
Class properties are initialized to defaults before the constructor is executed.
You will need to have the constructor use th TFeature passed in to set the property to an appropriate value.
  3 个评论
Chris
Chris 2023-1-28
编辑:Chris 2023-1-28
@Kevin Gjoni properties you want functions in your app to have access to need to be declared in the properties block:
properties
% ...
TFeature
end
Refer to them using their namespace:
app.TFeature = howeverYouMakeATFeature;
analyzer = FeatureAnalyzer(app.TFeature);
If you only need a variable within a single function, then it doesn't need to be a property of the app. For example, the iterator in a for loop.
for k = 1:3
% Do something
end
doesn't need to be app.k.
You've defined it in the function and it will be forgotten when the function exits.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by