简体   繁体   中英

calling several matlab commands from python using os.system

I just want to call two matlab commands from Python: the fist command just adds the folder and subfolders fo the desired path ( addpath(genpath('c:/file1/file2')) ), while the second command calls the function I want to use ( myfunction.m ).

I always used os.system("command") when I needed to call a function and os.system("command1 | command2") when I wanted to call two functions for example, so I tried:

os.system("matlab -r addpath(genpath('c:/file1/file2')) | matlab -r myfunction") 

But obviously this opens two matlab windows, the first one for the fist command and the second one for the second command. What I want is to call both commands in the same window, one after the other. However, if I try simply:

os.system("matlab -r addpath(genpath('c:/file1/file2')) -r myfunction")

it doesn't work...It performs the first command ( addpath ) but it does not call the function....

What I am doing wrong?? Any idea about how to do that?? I read lot of posts but they weren't very helpful, even if this is a very simple case.

Thanks in advance guys!! I really appreciate your help

The idea is to execute the following:

matlab.exe -r "addpath('c:\some\folder'); someFunction(); quit;"

Now you have to plug that inside a os.system call, and properly escape quotation marks...

For example, you can try:

>>> os.system("matlab.exe -nosplash -nodesktop -wait -r \"addpath('c:\\some\\folder'); someFunction(); quit();\"")

(note that backslashes and double-quotations are escaped, plus I added the -wait option so that the command doesn't return until MATLAB finishes execution).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM