I have 2 scripts:
python script
matlab script
I need to run this two scripts in parallel (no output for both of them). I was thinknig to call to the python scirpt, from the matlab script.
I know it is possible to run python script from matlab like:
systemCommand='my_script.py'
system(systemCommand)
however in this way, the matlab script will wait to the return of the python script, and the rest of my matlab script will not be executed.
any ideas?
As mentioned near the end of MATLAB's system documentation in the "Tips" section, to run a system command in the background (on *nix), you can append an ampersand(&) to the end of your command to tell it to run in the background.
system('my_script.py &')
If you're on Windows, you'll want to use the following to prevent a command window from opening.
system('start /b my_script.py');
Related
I need to run a MATLAB script from a Python script. I don't care about the output of it nor do I need to give it any arguments.
However, MATLAB R2016B's "engine" does not support Python 3.7 (Upgrading Matlab or down-grading python is not an option at this time)
So, I decided to make a shell script that runs it:
#!matlab -nodisplay -nodesktop -r 'try; myMatlabScript; catch; end; quit'
Now I need to run a bash script from Python. To do so, I did:
import subprocess
subprocess.call("./mybashscript.sh")
(And yes, the python script is at the same level as the shell script)
The python script does not complain directly. However, I do get the following:
'.' is not recognized as an internal or external command, operable program or batch file.
Which to me means that since Windows doesn't directly have bash, it doesn't know what to do with this shell script. I am not sure how to handle this. Some way to tell Python to use MSYS instead of Windows for the shell?
And thus the MATLAB script does not appear to run at all.
When I attempt under Linux (just for testing, I can't run it here for performance reasons), I get:
./mybashscript.sh: matlab: bad interpreter: No such file or directory
Is it possible this is because I didn't do the command addpath(genpath('.'))? If so, I'm not sure how I would do that in the shell script, and some help would be appreciated.
Or some other better solution would also be great.
1: Needed to re-name mybashscript.sh to mybashscript.bat
2: Needed to change the sub-process call to subprocess.call("mybashscript.bat") (as ./ was confusing the windows shell)
3: Needed to add the path properly. Here is what the batch script looked like:
matlab -nodisplay -nodesktop -r "addpath(genpath('C:/path/to/myscript')); myMatlabScript"
The double quotes are neccesary so the single quotes inside genpath do not cause it to end early.
And that was it!
EDIT: You can add -wait in the batch file to get the script to wait until it is complete before handing back to the Python script.
How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).
I am working on a machine learning program and attempting to perform experiments on the variables of my neural network. Due to Matlab's prowess with matrices, the learning is being performed in Matlab but for ease of use, the use of the learned outputs is in Python.
I need to make repeated calls to a Matlab file (currently not a function) and change a certain variable each time without me doing it manually. The Matlab script then outputs to a file read by the Python files. The Python file is responsible for the final output.
The big ideas is being able to set up a line of experiments and walk away from the computer. I could also see this perhaps being more easily done in another script if it is easy to make calls from one program to the other. I haven't done any work in Matlab until this summer and have only ever used the GUI shell.
Thank you for any help you can offer and let me know if more information is necessary.
There are several possible approaches for running MATLAB without a GUI and even without a MATLAB command line input:
start MATLAB in command line mode and call your script manually: matlab -nodesktop -nosplash
use a MATLAB to set the parameter and call your MATLAB script. this can be done by
using in terminal: matlab -nodesktop -nosplash -nodisplay -r myScript
use a Bash script to set the parameter and the call: matlab -nodesktop -nosplash -nodisplay -r "myscript"
(on Linux machines, for Windows you need to use the Power Shell)
You could then call Python from within your MATLAB scripts using the system() command to fetch your results.
Here is an example using a Bash script:
#!/bin/bash
# set parameter:
myParameter = 1
# call MATLAB with no interface at all and send it to background
# run MATLAB function with parameter, exit afterwards
# print command window output to a textfile
matlab -nodesktop -nosplash -nodisplay -r "myMatlabFunction(${myParameter});exit" > matlab_output.txt &
Is there a way to run a Windows command line from within SPSS?
I'm working with a program that is not supported by SPSS BEGIN PROGRAM procedures and I would like to run a command line from my SPSS syntax (I'm not interested in the output and I don't need to import/export any SPSS data I just want to run a cmd line from within SPSS).
To take a simpler example using freeware, let's say I want to run my R script at the end of my SPSS syntax but this would only work with a version not supported by SPSS (e.g. at the moment SPSS 21 supports R 2.14, let's say I absolutely need to run it on R 2.15 installed on my Windows PC), is there a way to run
R CMD BATCH C:\Files\MyRcode.R
within the SPSS syntax?
This is equivalent to running a program (in this case R) in batch mode. The program I need to use is not R, it's just an example.
As far as I'm aware SPSS can't do this but maybe using python (through SPSS BEGIN PROGRAM PYTHON) one can run a command line by using Python?
Thanks
P.S. For Python users who may not have used SPSS before one should in theory be able to run any Python code from within SPSS by using
BEGIN PROGRAM PYTHON.
Run some Python code
END PROGRAM.
Yes, you can do it with Python code:
BEGIN PROGRAM PYTHON.
import subprocess
subprocess.call(['C:\\R\\R.exe', 'CMD', 'BATCH', 'C:\\Files\\MyRcode.R'])
END PROGRAM.
Or, directly, with this:
HOST COMMAND=['R CMD BATCH C:\Files\MyRcode.R'].
Alternatives:
Put it in a batch file then execute it, for example using os.system().
Execute the Python or Basic script through the SCRIPT command.
I am looking for a way to execute ABAQUS (a program) automatically from a python script with certain conditions. In Linux's command (terminal) line I type in
abq683 cae script=XX.py
and it will have ABAQUS run the script. I am aware of the python module 'subprocess' and it can run the basic program, which is the 'abq683' part of the command, however I cannot seem to find a way to have the other conditions run with it.
I am wondering if there is a way to run the full abq683 cae script=XX.py command automatically from a python text file.
Perhaps there is a way to print characters to the terminal window's active line and run them?
I am using Python2.6 on a linux machine
Thank you for any help you may offer.
import subprocess
retcode = subprocess.call(['abq684', 'cae', 'script=XX.py'])