编译后运行matlab代码时出错

时间:2014-04-24 21:16:01

标签: matlab matrix compiler-construction executable

看起来这已被多次询问,但过去的帖子似乎都没有解决我的问题。所有这些都与矩阵/向量有关,而我的代码没有任何这些,只是简单的变量。它需要三个变量作为参数。它在Matlab环境中完美运行。当我使用mcc -m Normal.m编译它并尝试使用像这样的“./Normal 1 5 0.5”的可执行文件运行时,我只得到了错误。完整的错误消息是:

Error using /
Matrix dimensions must agree.

Error in Normal (line 4)



MATLAB:dimagree

抱怨第4行:N = 2 / dt,这有什么问题?

以下是代码:

function val=Normal(l1,l2,dt)

const=(l2/l1-1);
N=2/dt;

S=1.0/sqrt(l2/l1);
Z(1)=S;

for i=2:N
    t= -1+(i-1)*dt;
    Z(i)=1.0/sqrt(const*t*t+1);
    S=S+2*Z(i);
end
Z(21)=1.0/(l2/l1);
S=S+1.0/sqrt(l2/l1);

val=dt*S/2;


end

1 个答案:

答案 0 :(得分:3)

dt在通过命令./Normal 1 5 0.5传递到独立版时不是标量。它是一个包含3个元素的字符数组('0''.''5')!

将数字参数传递给独立的they are passed as strings时。因此,在函数内部,您需要将'0.5'转换为double,同样适用于l1l2

dt = str2num(dt);
l1 = str2num(l1);
l2 = str2num(l2);

请注意,您可以使用isdeployed在运行时确定该函数是否为独立函数:

if isdeployed, dt = str2num(dt); end

您可能需要显示结果:

if isdeployed, disp(val); end

结果:

>> system('Normal 1 5 0.5');
    1.4307
>> Normal(1,5,0.5) % .m function for comparison
ans =
    1.4307