matlabmatrixexecutable

Error running matlab code after compiling


It looks like this has been asked many times, but none of the past posts seem to solve my question. All those had to do with matrix/vector while my code does not have any of these, just simple variables. It takes three variables as arguments. It works perfectly fine within the Matlab environment. I only got the error when I compiled it with mcc -m Normal.m and tried to run with the executable like this "./Normal 1 5 0.5". The complete error message is:

Error using /
Matrix dimensions must agree.

Error in Normal (line 4)



MATLAB:dimagree

It is complaining about line 4: N=2/dt, what is wrong with this?

Here is the code:

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

Solution

  • But dt is not a scalar when passed into the standalone through the command ./Normal 1 5 0.5. It is a character array with 3 elements ('0', '.','5')!

    When passing numerical arguments to a standalone, they are passed as strings. Thus, inside the function, you need to convert '0.5' into a double, and similarly for l1 and l2:

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

    Note that you can use isdeployed to determine at runtime if the function is a standalone:

    if isdeployed, dt = str2num(dt); end
    

    And you might need to display the result:

    if isdeployed, disp(val); end
    

    Result:

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