Matlab 在类中使用常量属性时出现奇怪的错误
我试图将函数保存为类中的变量,以便以有序的方式访问它们。然而,每当我试图从下面的类中提取任何常量时,我都会得到下面的错误Matlab 在类中使用常量属性时出现奇怪的错误,matlab,matlab-class,Matlab,Matlab Class,我试图将函数保存为类中的变量,以便以有序的方式访问它们。然而,每当我试图从下面的类中提取任何常量时,我都会得到下面的错误 %FORMULAS Summary of this class goes here % Detailed explanation goes here properties (Constant) %F.heatCapacityOfLiquid t = @(z) z *2 end
%FORMULAS Summary of this class goes here
% Detailed explanation goes here
properties (Constant)
%F.heatCapacityOfLiquid
t = @(z) z *2
end
properties (Constant)
enthalpyChange = @(constants, temperatureIn, temperatureOut)integral(@(temperature)(@(constants, temperature)...
constants(1)...
+ temperature * constants(2)...
+ temperature.^2 * constants(3)...
+ temperature.^3 * constants(4)), 0,10);
heatCapacityOfLiquid = @(constants, temperature) constants(1)...
+ temperature * constants(2)...
+ temperature.^2 * constants(3)...
+ temperature.^3 * constants(4);
end
end
错误
>> F.t
Invalid default value for property 'enthalpyChange' in class 'F':
Error: Invalid use of operator.
不确定常量属性中的函数句柄,但如果您只想保存一些函数,为什么不将它们写为方法呢?因为抽象函数是对象,所以我希望访问这些对象时,中间没有方法。但我认为使用静态方法可以解决这个问题。你不能访问抽象对象,你只能构建一个从抽象对象继承的对象。这是一个与你的问题截然不同的概念。回到你的问题,不要停留在特定的术语上(
方法
,函数
,属性
)。您希望对象执行的是基于某个输入参数的计算并给出结果。这是MATLAB类定义中方法
块的最佳用例。(其他语言将允许您将函数/方法
定义为属性
,但MATLAB不是这样设计的)。此外,MATLAB中的静态
类和函数/方法不要求您实例化对象以访问该方法,所以访问这些计算可以直接在一个简单的方法调用中完成(一行代码)。我试着说匿名函数不是抽象函数,我失败得很惨,例如,我可以在另一个匿名函数中调用一个匿名函数来创建另一个抽象函数。我希望能够利用这种能力。