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.
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.
I learnt data manipulation and analysis through Stata and I used the log command to record all the commands written and the output generated. Doing so I could reproduce my findings, check previous results and share it with others in pdf or txt. What to use with Python? Is there a difference if I use a Python Jupyter Notebook or Spyder?
The way to do what you want is by using the %logstart command, as described here:
Log IPython output?
Stop using the python interpreter and start storing your commands in a text file before running them. This way you will be able to share, reuse, and revise your python code.
By executing the following command:
python code.txt
Python will read all of the lines in your file sequentially.
This is the main way python programs are executed, as a convention, python code files end with .py , so if you see a file with a .py extension, you can try executing it yourself using this method!
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');
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 &
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'])