2

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?

horchler
  • 18,384
  • 4
  • 37
  • 73
fyang
  • 89
  • 9

1 Answers1

1

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 '])'])
horchler
  • 18,384
  • 4
  • 37
  • 73
  • I don't understand this: `stats::swGOFT([' rStr '])`. It appears that the Mupad grammar is different to Matlab. Otherwise the `[' rStr ']` inside `stats::swGOFT()` doesn't make sense in Matlab. Perhaps this is why I didn't make it work in Mupad. But anyhow the code worked. Thanks! – fyang Apr 20 '15 at 14:33
  • Why there are spaces in front of and after rStr in `[' rStr ']`? I deleted the spaces and this `['rStr']` didn't work. So, `[' rStr ']` is equivalent to a list of numbers, e.g. `1, 2, 3, 4`. Tricky indeed. – fyang Apr 20 '15 at 14:42
  • @fyang: It's not `stats::swGOFT([' rStr '])`, but `['stats::swGOFT([' rStr '])']`. It's not tricky, it's just string concatenation. You're not calling `stats::swGOFT` directly, but rather telling `feval` or `evalin` to call it in the right context. You have to tell them the name of the function to call and specify any arguments. `feval` calls `evalin` under the hood in this case so it may be slightly more efficient to use it directly, but you have to pass it one long string. A bit [more here](http://mathworks.com/help/symbolic/call-built-in-mupad-functions-from-the-matlab-command-window.html). – horchler Apr 20 '15 at 15:34
  • If it helps, you can put commas in to further distinguish the elements of the character array: `['stats::swGOFT([', rStr, '])']`. This is equivalent to using [`strcat`](http://mathworks.com/help/matlab/ref/strcat.html) explicitly: `strcat('stats::swGOFT([', rStr, '])')`. Try printing out `rStr` and `['stats::swGOFT([' rStr '])']` for a small vector if you're still confused. Here's a bit more on [working with strings](http://mathworks.com/help/matlab/matlab_prog/creating-character-arrays.html) in Matlab. – horchler Apr 20 '15 at 15:47
  • Thanks! After you put the commas, I realised what you meant. It's the Matlab grammar. Just to concatenate 3 strings. – fyang Apr 21 '15 at 13:44