How to call matlab function/script in ssh? - matlab

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?

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.

Related

Pass file name as a string through command line into Maple

I'm trying to use Maple function in external program using command-line interface. Data for function is to be passed through file. For demonstration of the problem I created two files: /home/user_name/test.mpl and /home/user_name/test_data.txt.
test.mpl ("cat" demonstrates use of Maple function):
#filename := "/home/user_name/test_data.txt":
print(filename):
i := parse(readline(filename)):
poly := parse(readline(filename)):
s := parse(readline(filename)):
print(cat(convert(poly+i,string), " ", s)):
test_data.txt :
1
x^2 * y + 1
"A string."
According to the manual, I can use something like this (but this example doesn't cover usage of two files, one as a code and another as an argument):
/usr/local/maple/bin/maple -c 'datafile:="/tmp/12345.data";' -c N:=1;
When I try
/path/to/maple -c 'filename:="/home/user_name/test_data.txt":' -q /home/user_name/test.mpl
I get the following error:
Error, incorrect syntax in parse: `/` unexpected (near 11th character of parsed string)
If I delete first / in filename string, I get the following output (before the errors related to readline):
/ home \
|-------------------| . txt
\user_name test_data/
It clearly demonstrates that file path is not parsed as a string (but probably as some kind of expression). Probably I should use some escape sequences, for Maple or for shell, but none of my attempts worked.
If I get file name inside test.mpl (uncommenting first line there and removing -c parameter), it works though, but that's not what I need.
How to pass file name as a string through command line (probably not with using -c)?
It works for me using commandline Maple on Linux, as say,
/path/to/maple -c 'filename:=\"/home/user_name/test_data.txt\":' -q /home/user_name/test.mpl

Passing output-values from a C-program to MATLAB (via batch-file)

I have a DOS batch-file MYDOS.BAT containing:
My C-application (myApp.exe) reading an input-file (inputFile) from the DOS-command line
myApp.exe analyses inputFile and exits/returns with a code=N
How can I pass value N into my MATLAB script?
E.g: MYDOS.BAT is run by DOS>MYDOS inputFile and contains the following lines:
myApp %1
echo %ERRORLEVEL%
set samplerate=%ERRORLEVEL%
echo %samplerate%
...
C:/... matlab mymatlab.m ...
HOW CAN I THEN PASS the value %samplerate% into my mymatlab.m script?
You can use the system command to use DOS commands in Matlab. Use doc system to see the documentation.
System can have 2 outputs. The first one is a status, which will tell you if the operation succeeded. The second one is the output to the command prompt. You can parse this to get your value. You could use the following code as a guide for your situation:
[status,cmdout]=system('MYDOS.bat');
cmdout will contain the string that you are echoing to the command prompt.

Read command output line by line in sh (no bash)

I am basically looking for a way to do this
list=$(command)
while read -r arg
do
...
done <<< "$list"
Using sh intead of bash. The code as it is doesn't run because of the last line:
syntax error: unexpected redirection
Any fixes?
Edit: I need to edit variables and access them outside the loop, so using | is not acceptable (as it creates a sub-shell with independent scope)
Edit 2: This question is NOT similar to Why does my Bash counter reset after while loop as I am not using | (as I just noticed in the last edit). I am asking for another way of achiving it. (The answers to the linked question only explain why the problem happens but do not provide any solutions that work with sh (no bash).
There's no purely syntactic way to do this in POSIX sh. You'll need to use either a temporary file for the output of the command, or a named pipe.
mkfifo output
command > output &
while read -r arg; do
...
done < output
rm output
Any reason you can't do this? Should work .. unless you are assigning any variables inside the loop that you want visible when it's done.
command |
while read -r arg
do
...
done

Read Arguments From CMD and Concatenate Strings in Bat File

I have the following code in my bat file which saved as MyLM.bat:
#echo off
matlab -automation -r "addpath('C:\Users\mojtaba\BrainModel');AddPathes;MyLM('MT5Test_LM')" > matlab_output.log
exit
In which i simply add the main path and then necessary paths and then i run my function (Which is MyLM). I am running the following code from my matlab command prompt :
!start "MATLAB test" /Min /B MyLM.bat
and it works fine and i am happy! So i can run different instances of matlab separately using different bat files. What will makes me happier is that i can pass my argument (which is 'MT5Test_LM') from matlab command prompt. So i dont need to save different bat files. What i actually need is to have some code like this :
!start "MATLAB test" /Min /B MyLM.bat 'MT5Test_LM'
then i need some piece of codes in my bat file to read this argument and concatenate some strings.
Is there any suggestion?
Have you tried using the input argument of the batch file (%1)?
See, e.g., this manual on batch file input argument.
You might want your bathc file to look like
matlab -r "myLM( %1 )"

run Matlab in batch mode

It seems to me that there are two ways to run Matlab in batch mode:
the first one:
unset DISPLAY
matlab > matlab.out 2>&1 << EOF
plot(1:10)
print file
exit
EOF
The second one uses option "-r MATLAB_command":
matlab -nojvm -nosplash -r MyCommand
Are these two equivalent?
What does "<< EOF" and the last "EOF" mean in the first method?
Thanks and regards!
The first method simply redirects the standard output > matlab.out and the standard error 2>&1 to the file matlab.out.
Then it uses the heredoc way of passing input to MATLAB (this is not specific to MATLAB, it is a method of passing multiple lines as input to command line programs in general).
The syntax is << followed by an unique identifier, then your text, finally the unique id to finish.
You can try this on the shell:
cat << END
some
text
multiple lines
END
The second method of using the -r option starts MATLAB and execute the statement passed immediately. It could be some commands or the name of a script or function found on the path.
It is equivalent to doing something like:
python -c "print 'hello world'"
Refer to this page for a list of the other start options.