Construtor does not find external functions

1 回表示 (過去 30 日間)
PolynomialChaos
PolynomialChaos 2017 年 3 月 23 日
回答済み: Steven Lord 2017 年 3 月 23 日
The constructor of a Class does not find a function in an .m file as soon as I move the function .m file to the Class folder, where the Class .m file is. It works as long as the function .m file is in the main folder. What do I have to do so that the constructor finds the fctn in the Class folder?
The error message is: Undefined function 'fctn' for input arguments of type 'double'.
classdef Data
properties
propA;
end
methods
function obj = Data (attrA)
[obj.propA] = fctn(attrA);
end
end
end

採用された回答

Matt J
Matt J 2017 年 3 月 23 日
You could make a private/ folder in the class folder and put your function in there. You could also place the function in the classdef file as a subfunction.

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 3 月 23 日
If you want fctn to be a method of class Data that is defined outside the classdef file, you need to define it as a method in the class file itself, then write the implementation in the separate file. See this section of the documentation for more information.
If you want fctn to be a function, not a method, that's accessible to the class but you don't want it to be visible to any other function or class you'll probably want to make it a private function. See the section titled "Access to Functions Defined in Private Folders" in this documentation page. Alternately you could make it a class-related function in the file itself.

カテゴリ

Help Center および File ExchangeClass Introspection and Metadata についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by