Search code examples
matlabstatisticssymbolic-mathmupad

How to pass a Matlab vector variable to a MuPAD function?


I want to use the stats::swGOFT function in MuPAD. I have a numerical vector called r. I used

feval(symengine, 'stats::swGOFT', r); 

The error was

Error using mupadengine/feval (line 157)
MuPAD error: Error: Some data are of invalid type.

So I tried a more direct way, which worked:

feval(symengine, 'stats::swGOFT', 1,2,3,4);

But this didn't work:

feval(symengine, 'stats::swGOFT', [1,2,3,4]);

My variable r is a 1146-by-1 double vector. Obviously I can't manually input all the numbers. So, how to pass the vector variable r to the MuPAD function stats::swGOFT?


Solution

  • MuPAD is not Matlab. From the current version documentation for stats::swGOFT, it appears that this function requires a list, as opposed to an array (what Matlab uses). Many MuPAD functions automatically coerce inputs to the desired format, but that doesn't seem to occur in this case. You have several options if you want to call this function from Matlab using numeric values – here's a simple one that will work for both floating point and symbolic numeric values:

    r = randn(1146,1);
    rStr = char(sym(r(:).'));
    feval(symengine, 'stats::swGOFT', rStr(9:end-2))
    

    This one should perform the string conversion more quickly for large datasets of floating point values using sprintf:

    r = randn(1146,1);
    rStr = ['[' sprintf('%.17g', r(1)) sprintf(',%.17g', r(2:end)) ']'];
    feval(symengine, 'stats::swGOFT', rStr)
    

    Since you're converting to a string yourself, you might as well convert the above to use evalin directly:

    r = randn(1146,1);
    rStr = [ sprintf('%.17g', r(1)) sprintf(',%.17g', r(2:end)) ];
    evalin(symengine, ['stats::swGOFT([' rStr '])'])