coder.ceval 的输出类型未知
问题
您会看到以下错误消息:
Output of 'coder.ceval' has unknown type.The enclosing expression cannot be evaluated.Specify the output type by assigning the output of 'coder.ceval' to a variable with a known type.('coder.ceval' 的输出具有未知类型。无法计算外围表达式。请将 'coder.ceval' 的输出赋给具有已知类型的变量来指定输出类型。)
原因
当代码生成器无法确定 coder.ceval 调用的输出类型时,会出现此错误消息。
解决方法
用预期的输出类型初始化一个临时变量。将 coder.ceval 的输出赋给此变量。
示例
假设您有一个名为 cFunctionThatReturnsDouble 的 C 函数。您要为函数 foo 生成 C 库代码。代码生成器返回错误消息,因为它无法确定 coder.ceval 的返回类型。
function foo %#codegen callFunction(coder.ceval('cFunctionThatReturnsDouble')); end function callFunction(~) end
要修复该错误,请使用一个临时变量定义 C 函数输出的类型。
function foo %#codegen temp = 0; temp = coder.ceval('cFunctionThatReturnsDouble'); callFunction(temp); end function callFunction(~) end
您还可以使用 coder.opaque 来初始化该临时变量。
使用类的示例
假设您有一个具有自定义 set 方法的类。此类使用 set 方法来确保对象属性值在某个范围内。
classdef classWithSetter properties expectedResult = [] end properties(Constant) scalingFactor = 0.001 end methods function obj = set.expectedResult(obj,erIn) if erIn >= 0 && erIn <= 100 erIn = erIn.*obj.scalingFactor; obj.expectedResult = erIn; else obj.expectedResult = NaN; end end end end
为函数 foo 生成 C 库代码时,代码生成器生成错误消息。set 方法的输入类型无法确定。
function foo %#codegen obj = classWithSetter; obj.expectedResult = coder.ceval('cFunctionThatReturnsDouble'); end
要修复该错误,请用已知类型初始化临时变量。对于此示例,使用双精度标量类型。
function foo %#codegen obj = classWithSetter; temp = 0; temp = coder.ceval('cFunctionThatReturnsDouble'); obj.expectedResult = temp; end