applying unix commands in matlab

I have been trying to apply unix commands in the MATLAB environment but it is not working. For example, if I enter this simple command to display the environment variable PATH, the following is the output:
[status, cmdout]=unix('echo $PATH')
status =
0
cmdout =
$PATH
When I apply this same command in cygwin, it gives me a long output pointing to the correct directory. What do I need to do to fix this?

3 件のコメント

Walter Roberson
Walter Roberson 2013 年 8 月 14 日
What shell are you using?
[status, cmdout] = unix('printenv SHELL')
You might have to use
[status, cmdout] = unix('env | grep SHELL')
Also which operating system are you using?
artsci4
artsci4 2013 年 8 月 14 日
When I apply
[status, cmdout] = unix('printenv SHELL')
cmdout stores 'printenv' is not recognized as an internal or external command, operable program or batch file.
Same with applying: [status, cmdout]=unix('env | grep SHELL')
then it says 'env' is not recognized...
I am using Windows 7. And the MATLAB version is R2012b.
Daniel Shub
Daniel Shub 2013 年 8 月 14 日
I don't have the documentation in front of me, but I am pretty sure the MATLAB UNIX command is not magically and does not give you access to UNIX from a Windows machine.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2013 年 8 月 14 日

0 投票

The unix() command does not deliberately pass the command to a Unix command shell. The unix() command and system() command and dos() command are identical (or very nearly so), and all three of them pass the command to the "system shell". Which, on MS Windows, is "cmd.com" or "cmd.exe" or whatever the 64 bit equivalent name is.
If I recall old MS Windows information properly,
system('print PATH')
Note: if you are calling to the system just to look at environment variables, then instead use MATLAB's getenv()
getenv('PATH')

7 件のコメント

artsci4
artsci4 2013 年 8 月 14 日
Thank you for your answer Walter. It makes sense. So, how do I pass the commands in MATLAB to an Unix command shell? What I really want to do in MATLAB is this actually:
for i unix(['./a.exe', FileIn(i) '>',FileOut(i)]); end
I want to apply an application on the fileIn and then store the output in fileOut. This command has to be passed to an Unix command shell. And I want to use MATLAB to iterate in a loop to change FileIn and FileOut automatically every time it iterates in the loop.
Walter Roberson
Walter Roberson 2013 年 8 月 15 日
system(sprintf('./a.exe ''%s'' > ''%s''', FileIn{i}, FileOut{i})
This will work on MS Windows, Linux, and OS-X. Note, though, that on MS Windows it will not be passed to a Unix command shell: it will be passed to CMD.EXE or the like, which is able to understand redirection of stdout
artsci4
artsci4 2013 年 8 月 15 日
So since I am using Windows 7, there is no way that I can make this work?
Walter Roberson
Walter Roberson 2013 年 8 月 15 日
Why is it necessary to send it to a Unix shell? What behavior are you expecting from a Unix shell that you cannot get from the MS Windows command shell ?
artsci4
artsci4 2013 年 8 月 15 日
編集済み: artsci4 2013 年 8 月 15 日
Actually, I am able to run the code. I can see the output file named FileOut being created in the designated folder. But that file is 0KB big. In other words, the output of the command is not being stored in the output file. And now in the MATLAB command window, it reads: '.' is not recognized as an internal or external command, operable program or batch file.
Thank you for your help and patience.
artsci4
artsci4 2013 年 8 月 15 日
I have tried this same command in cygwin terminal and the output would be successfully stored to the output file.
Walter Roberson
Walter Roberson 2013 年 8 月 15 日
編集済み: Walter Roberson 2013 年 8 月 15 日
system(sprintf('a.exe ''%s'' > ''%s''', FileIn{i}, FileOut{i})

サインインしてコメントする。

その他の回答 (1 件)

artsci4
artsci4 2013 年 8 月 16 日

0 投票

I fixed the problem. I fixed it apparently by adding the path to my current directory to the Windows Environment Variable PATH. Then the system recognizes the executable file.

カテゴリ

ヘルプ センター および File ExchangeFiles and Folders についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by