Call a Unix script with MATLAB: how is it different from using Cygwin?

Hello everybody, i just subscribed to MATLAB central, even though i read you from years :)
My problem is the following: I am working with a free Automatic Speech Recognition toolkit, called HTK, which works both on Unix and Windows environment.
I work on a computer with Windows 7 (x64) and MATLAB 2010a installed and I tried to use the toolkit by calling the commands from both the Windows command line and from the Cygwin bash shell.
In both the cases it works but i realized that some commands work in Cygwin whereas they don't in Windows (i suppose it is because the toolkit was natively developed to be used in a Unix environment).
I then moved to MATLAB to call the different scripts.
The problems I noticed when calling the HTK functions from MATLAB using the dos command are (as expected) the same I get from the Windows command line.
So, I tried to use the command unix, expecting the positive results obtained with Cygwin, but instead the same thing happening with dos and the Windows command line occurs.
Thus, i guess that using unix in MATLAB is not the same as calling whatever command withing Cygwin.
I've never worked with unix programs before, so it might be a simple thing to work out. Nevertheless, i try to look for this problem both here and on other forums and i could find what i needed...
I hope I expressed my problem in a comprehensible way xD
Giuliano

回答(3 个)

I realize this thread is dead, but I have been looking to solve a very similar problem. The result I have settled on for my problem is to call cygwin's bash from the windows command line. The command call to cygwin's bash also includes the bash command I want to execute.
The syntax is the following. Please experiment per your purposes.
dos('C:\cygwin\bin\bash.exe --login -c "[command]"')
where the quantity [command] should be replaced per your need.
Hope this helps,
David

2 个评论

Hey David, thanks for the reply. Unfortunately I'm not working within the same environment anymore so I don't think I'll have the chance to check your solution... At the time, I just bypassed the issue by working on Linux directly.
What should I do about the "Accept this answer" issue?!
this answer worked for me. i had seen this answer before but without the quotes. in my case, i needed them. thanks!

请先登录,再进行评论。

See the documentation for the "unix" command:
The MATLAB software uses a shell program to execute the given command. It determines which shell program to use by checking environment variables on your system. MATLAB first checks the MATLAB_SHELL variable, and if either empty or not defined, then checks SHELL. If SHELL is also empty or not defined, MATLAB uses /bin/sh.
Hmmmm... Doesn't that only refer to the case when one runs MATLAB scripts in a Unix environment? I want to run unix scripts in a Unix environment (via Cygwin) using MATLAB.
But perhaps it's just me not being able to understand xD :S

7 个评论

No, the documentation specifically states that you can use any of dos() or system() or unix() in any of the environments.
The unix() command documents how it figures out what the shell is. If you command your Matlab to
getenv('SHELL')
you will see what shell is being used. It will probably be the standards Windows CMD.EXE
Matlab does not provide a mechanism for emulating unix command interpretation itself. Using unix() will not run the command with unix interpretation -- not unless your SHELL is set to a unix-style command interpreter.
In particular, you can use
setenv('SHELL', path_to_cygwin_shell)
before calling unix()
Thank you very much for the answer.
Anyway, I tried to set the SHELL variable in MATLAB (as well as the MATLAB_SHELL variable) to the cygwin shell's path, but the result is the same as before...
It is like the cygwin shell is not actually used.
I don't really get it...
I found that changing the SHELL environment variable within Matlab did not affect the shell used, but that if I set MATLAB_SHELL in the environment before starting Matlab then its value was used.
Thanks again, however i haven't found out what the problem is yet...
I changed the MATLAB_SHELL from the control panel, and after rebooting (which is necessary to confirm the changes for environment variables) the value of MATLAB_SHELL is correctly changed but the error returned from MATLAB remains...
I tried both with MATLAB_SHELL and SHELL (both using values "C:\cygwin\bin" and "C:\cygwin\bin\bash.exe")
Any other suggestions?! Otherwise i think i'll just have to drop the idea to make this to work... :S
Try:
>> !echo %MATLAB_SHELL%
from the MATLAB prompt and make sure that MATLAB picks up the environment variable correctly.
The shell would definitely have to be the full path, not just the directory.
I do not have an operational Windows system to test with these days, sorry.
@Kaustubha: yes, the environment variable is correctly set.
@Walter: tried with full path "C:\cygwin\bin\bash.exe" and also with the login option "C:\cygwin\bin\bash.exe --login" (just to make an attempt) but nothing.
Well, if you guys have news just let me know! :D

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

提问:

2011-2-27

评论:

2015-5-8

Community Treasure Hunt

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

Start Hunting!

Translated by