Effacer les filtres
Effacer les filtres

Running multiple Matlab commands in only one terminal from command prompt

52 vues (au cours des 30 derniers jours)
Joao Furtado
Joao Furtado le 23 Avr 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!

Réponses (2)

Clayton Gotberg
Clayton Gotberg le 24 Avr 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 commentaires
Joao Furtado
Joao Furtado le 26 Avr 2021
Hello, Mr. Gotberg! How are you? I hope you're doing well!
Thank you very much for your answer.
What I am trying to develop is a code that must be capable to calculate some trajectories, which means it must be point-to-point, and it also depends of the embedded system accoupled to it. This function will receive the system's actual position, read from an encoder connected to the network. That's why we're calling the Matlab function from the Windows terminal!
So, for each point, the Matlab function must be called once. It means that, for the next point, we must call it again.
The problem about this method is that, for each Matlab function calling, the Matlab program starts and demands a few seconds to initialize - in my case, it is taking about 6 or 7 seconds. My trajectory is composed by millions of points, and I can't wait 6 seconds for each one. That's why we need to keep the Matlab terminal opened from the first point to the last one, and not to be opening/initializing it every time.
I hope I was clear on this explanation!
Regards, and have a nice week!
Clayton Gotberg
Clayton Gotberg le 26 Avr 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.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 26 Avr 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.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by