MATLAB Answers

Running multiple Matlab commands in only one terminal from command prompt

9 views (last 30 days)
Joao Furtado
Joao Furtado on 23 Apr 2021
Answered: Walter Roberson on 26 Apr 2021
Hello, everyone!
When we run a Matlab file from Windows command prompt, we generally use the following command line: (note the command is very simple, and it's just to illustrate the proceeding I am trying to develop)
matlab -nodesktop -r "a = 2"
Is there any way to, in another command through Windows command prompt, send a Matlab command (for example, fprintf('%d\n',a)) to the same terminal we've opened? Because if I type the following command:
matlab -nodesktop -r "fprintf('%d\n', a)"
it opens another terminal, just like the first one, and the variable a can't be read because there is no value associated to it !!
What I want to do is to send the fprintf command to the first Matlab terminal I've opened!
I hope my question is clear to everyone! =D
Thanks in advance!

Answers (2)

Clayton Gotberg
Clayton Gotberg on 24 Apr 2021
On Linux I'm able to do exactly what you want with the -nodesktop -nosplash -nojvm options - it takes over the terminal window to let me send commands as if it's the MATLAB command line - but it doesn't seem possible on Windows.
When I executed your first statement, it opened a MATLAB command line window. If you're already entering something via command prompt, is there any reason you're not switching the window? If you know what code you'll be running in advance, why not write a script to execute with the run command?
  2 Comments
Clayton Gotberg
Clayton Gotberg on 26 Apr 2021
Yes, your goal makes sense!
It sounds like you've written a batch file (or other command prompt script) to read in some value from the network, then pass it to MATLAB.
Does this trajectory analysis need to be in real-time? If not, just change the script to save the position value to a text file, then ask MATLAB to perform the analysis on that text file. You can read the contents of a text file with fscanf and a number of other functions.
If it needs to be in real-time, your current workflow of
  1. Read in data
  2. Call MATLAB to send it the value
  3. Repeat
is always going to have you wait 6-7 seconds between points, because MATLAB assumes you want to start a new instance as you have noted. Instead, try bringing the system commands into MATLAB! You can run them using the shell escape or the system function.

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Apr 2021
MATLAB for Windows offers the Automation Server interface.
All MATLAB versions offer the Engine API so they can be called from other programs.
With Windows in particular, you may be able to use .NET System Diagnostic.Process to start MATLAB and connect other processes to input.
For Mac and Linux, you can create Named Pipes that look like files that you can read and write.

Community Treasure Hunt

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

Start Hunting!