0

In a file S.m I store:

function s = S(a, b)
    s = a + b;

I try to call the function from ssh shell like this:

matlab -r -nodisplay "S 3 5" > sum.txt

and I get a "not enough arguments in input" error. Can anyone see the reason for that?

Drimades Boy
  • 437
  • 1
  • 5
  • 19
  • Possible duplicate of [Running a matlab program with arguments](https://stackoverflow.com/questions/8981168/running-a-matlab-program-with-arguments) – Cris Luengo Mar 21 '18 at 20:01
  • 2
    Your error is that the `"S 3 5"` part should come right after `-r`: `matlab -nodisplay -r "S 3 5"` – Cris Luengo Mar 21 '18 at 20:02
  • 4
    Also, `a` and `b` in your function will be strings if you call `S` this way. Use `str2double` to convert them to numbers. – Cris Luengo Mar 21 '18 at 20:03
  • @CrisLuengo: Maybe write that into an answer so the question can be marked as answered? – Argyll Mar 25 '18 at 03:12
  • @Argyll: OK, I did. – Cris Luengo Mar 25 '18 at 14:44
  • @CrisLuengo I upvoted your answer. Btw, do you happen to know starting with what version of matlab this would work? I used to try it with the 2014a version on a windows machine and it never worked for me – Argyll Mar 25 '18 at 23:42
  • @Argyll: I've done this under Windows way back in the late 90's, with version 5.3. I have not used Windows a whole lot in the last 10 years, but I don't think this has changed. I added the reference to the Windows command line in the answer. – Cris Luengo Mar 26 '18 at 00:48
  • @CrisLuengo: See [this answer](https://stackoverflow.com/a/2669790/3181104). It seems i am not the only one who has had issues with running matlab by ssh'ing into a windows machine – Argyll Mar 27 '18 at 01:03
  • @Argyll: I didn't even know it is possible to SSH into a Windows machine... Windows is not designed for text-only interaction, I'm not surprised of the problems they describe in that answer. I ran MATLAB from from batch scripts locally, not remotely. I've only ever run MATLAB remotely on Solaris and Linux. Sorry I can't be of more help in your situation! – Cris Luengo Mar 27 '18 at 01:56
  • @CrisLuengo No worries! – Argyll Mar 27 '18 at 10:48

1 Answers1

1

The -r argument introduces the command to execute. The two cannot be separated by other arguments. That is, you can write

matlab -nodisplay -r "S 3 5" > sum.txt

or

matlab -r "S 3 5" -nodisplay > sum.txt

to get MATLAB to run the function S with input arguments '3' and '5'. See the official documentation of the UNIX command matlab for more information. There is a separate documentation page for Windows, where different options are allowed, but the -r switch should still work.

Do note that the MATLAB statement

S 3 5

is equivalent to

S('3','5')

That is, the arguments are seen as strings. Convert them to numbers using the str2double function.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120