当你编译 matlab 代码时:
function [out1, out2] = epidemic(in1, in2, in3)
%[
...
%]
对于独立 ( mcc -m epidemeic.m
),Matlab 以某种方式生成以下伪 c 代码并将其编译为 .exe:
int main(int argc, char** argv)
{
// Load compiled code produced by mcc
HMCRInstance* hInst = loadByteCodeProducedByMccFromResources();
// Similar to have wrote in matlab "epidemic(argv[0], argv[1], ...)"
// 1) Without asking for any argument output
// 2) Argument inputs are passed as strings
int errorCode = mclFevalFromExeArg(hInst, "epidemic", argc, argv);
return errorCode; // only indicates if call to 'mclFEvalFromExeArg'
// succeded, it does not relate to out1, out2 at all.
}
注意:如果您想查看 mcc 生成的确切代码,请使用mcc -W main -T codegen epidemic.m
所以,直接编译成独立的,你不能使用你的 Matlab 函数的输出。如果您需要使用 的输出参数epidemic
,要么
注意:当您将代码编译到共享库时,mcc
将生成一个导出以下函数的 dll:
extern LIB_epidemeic_C_API
bool MW_CALL_CONV mlxEpidemic(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
nrhs/prhs
是输入参数的数量及其值(作为mxArray
类型)。并且nlhs/plhs
是您在调用时想要的输出参数epidemic
。由您在 mxArray 和等效的 C 本机类型之间进行编组。
编辑
正如您所指出的那样,它epidemic
返回一个值向量,您可以像这样从独立中显示它们:
function [output] = epidemic(v1, v2, v3)
%[
% When called from system cmd line, v1, v2, v3 are passed
% as string. Here is how to convert them to expected type if required
if (ischar(v1)), v1 = str2double(v1); end
if (ischar(v2), v2 = str2double(v2); end
if (ischar(v3)), v3 = str2double(v3); end
...
output = ...;
...
if (isdeployed())
disp(output);
end
%]