how can I send commands to 'command window' programly?

Hello. I want to send "commands" to matlab command window programly
  1. I opened Matlab "Command window", in mac (Linux) type 'matlab -nodesktop' in terminal.
  2. I want to send matlab commends to matlab console (command window) in another terminal, or python script.
  3. because, i want to compare the results by matlab version 2010 and 2017, so i will open both version of matlab with no desktop, and run same script in each version using python. how could i do that?

 Accepted Answer

matlab -nodesktop -r "try; NAME_OF_SCRIPT; catch; end; quit"
is probably the easiest to use, where NAME_OF_SCRIPT is a .m file that has already been created.
If you need to work with input that is not defined ahead of time, then create a named pipe and
matlab -nodesktop < NAMED_PIPE_FILENAME
and write to the named pipe in the place you are generating the input.
If your input generation is by way of a shell derived from the Bourne Shell, then you can use exec and numbered streams and & to start background jobs that you can write to selectively by using the correct output stream number redirection. You would go through that trouble if you were working with the two different MATLAB sessions simultaneously. If you were only working with one session at a time, then you would probably instead use |& to create a co-process and use print -p to send commands to it and read -p to read from it. Which is not to neglect the power of piping the output of a shell script into MATLAB:
./SHELL_SCRIPT_NAME | matlab -nodesktop

6 Comments

Thank you for your answer.
I also found your first method, but that run matlab and quit again and again in every time. It is inefficient and too havey.
so, i want to run matlab in "command window" mode, and just send commands when I want. could i do that?
Your choices are
  1. redirection from a named pipe
  2. redirection from a numbered file descriptor using sh or derivative
  3. shell co-process
  4. pipe into matlab stdin
  5. java robot class
  6. pseudoterminal pty
Thanks for your help, it is little difficult but i could got clue.
I will study your solution. thanks!
You could also set up matlab as a tcp server and have it listen for connections and receive commands and execute and return values .
If you were using Windows then there would also be the option of using the COM Automation Server interface .
Walter, could you please extend your solution 1, i.e. launch Matlab with named pipe.
My bash code is this:
mkfifo my_pipe
matlab -nodesktop < my_pipe
echo "ls" > my_pipe
But Matlab won't receive my command.
Thanks!
I just did a test with
cd /tmp
mkfifo /tmp/myfifo
/Applications/MATLAB_R2019a.app/bin/matlab -nojvm -nodesktop < /tmp/myfifo
then in a different shell window
echo ls >> /tmp/myfifo
The ls did get executed by MATLAB. Which then crashed. And there was no visible output until the crash.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!