Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Running shell command using system

Subject: Running shell command using system

From: Philipp Steffen

Date: 7 Jan, 2011 10:52:05

Message: 1 of 7

Hi all,

I have the following problem: I would like to run a shell command from within Matlab but I just receive the error "No such file or directory".

I did the following:
in the MacOS terminal (bash): git --> runs the executable
in Matlab: system('git') gives the error

If I change the working directory in Matlab to the directory of the executable (/usr/local/git/bin), the following command works !./git.
Why do I have to change the working directory to the directory of the executable? Is there a way around this?

Thanks in advance,
Philipp

Subject: Running shell command using system

From: Walter Roberson

Date: 7 Jan, 2011 17:51:33

Message: 2 of 7

On 11-01-07 04:52 AM, Philipp Steffen wrote:

> I have the following problem: I would like to run a shell command from within
> Matlab but I just receive the error "No such file or directory".
>
> I did the following: in the MacOS terminal (bash): git --> runs the executable
> in Matlab: system('git') gives the error
>
> If I change the working directory in Matlab to the directory of the executable
> (/usr/local/git/bin), the following command works !./git.
> Why do I have to change the working directory to the directory of the
> executable? Is there a way around this?
> Thanks in advance,

Add /usr/local/git/bin to your system path. That would be your PATH
environment variable, which you can alter outside of Matlab, or which you can
alter using getenv('PATH') and putenv('PATH', newvalue)

Subject: Running shell command using system

From: the cyclist

Date: 11 Jan, 2011 21:36:05

Message: 3 of 7

Walter Roberson <roberson@hushmail.com> wrote in message <ig7jt5$56s$2@nrc-news.nrc.ca>...
> On 11-01-07 04:52 AM, Philipp Steffen wrote:
>
> > I have the following problem: I would like to run a shell command from within
> > Matlab but I just receive the error "No such file or directory".
> >
> > I did the following: in the MacOS terminal (bash): git --> runs the executable
> > in Matlab: system('git') gives the error
> >
> > If I change the working directory in Matlab to the directory of the executable
> > (/usr/local/git/bin), the following command works !./git.
> > Why do I have to change the working directory to the directory of the
> > executable? Is there a way around this?
> > Thanks in advance,
>
> Add /usr/local/git/bin to your system path. That would be your PATH
> environment variable, which you can alter outside of Matlab, or which you can
> alter using getenv('PATH') and putenv('PATH', newvalue)

Alternatively, you can specify the complete pathname in the MATLAB command line:

>> system('/usr/local/bin/git')

(Coincidentally, I am just starting to explore using git myself.)

Subject: Running shell command using system

From: Philipp Steffen

Date: 11 Jan, 2011 22:01:07

Message: 4 of 7

Hi Walter, hi Cyclist,

thanks for your help. I did not know that I can alter the system PATH from within MATLAB. The easiest way for me though is to use the full path in system().
Best,
Philipp

"the cyclist" wrote in message <igiig5$sbj$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <ig7jt5$56s$2@nrc-news.nrc.ca>...
> > On 11-01-07 04:52 AM, Philipp Steffen wrote:
> >
> > > I have the following problem: I would like to run a shell command from within
> > > Matlab but I just receive the error "No such file or directory".
> > >
> > > I did the following: in the MacOS terminal (bash): git --> runs the executable
> > > in Matlab: system('git') gives the error
> > >
> > > If I change the working directory in Matlab to the directory of the executable
> > > (/usr/local/git/bin), the following command works !./git.
> > > Why do I have to change the working directory to the directory of the
> > > executable? Is there a way around this?
> > > Thanks in advance,
> >
> > Add /usr/local/git/bin to your system path. That would be your PATH
> > environment variable, which you can alter outside of Matlab, or which you can
> > alter using getenv('PATH') and putenv('PATH', newvalue)
>
> Alternatively, you can specify the complete pathname in the MATLAB command line:
>
> >> system('/usr/local/bin/git')
>
> (Coincidentally, I am just starting to explore using git myself.)

Subject: Running shell command using system

From: the cyclist

Date: 11 Jan, 2011 22:31:04

Message: 5 of 7

"Philipp Steffen" wrote in message <igijv3$2c4$1@fred.mathworks.com>...
> Hi Walter, hi Cyclist,
>
> thanks for your help. I did not know that I can alter the system PATH from within MATLAB. The easiest way for me though is to use the full path in system().

Possibly the best long-term solution is to add the following to your startup.m file, which will set the path at the beginning of each MATLAB session:

%%%%%%%%%%%%%%%%%%%
% Add directories to the system path (primarily to get "git" to work?)
setenv('PATH', [getenv('PATH') ':/usr/local/bin'])
%%%%%%%%%%%%%%%%%%%


[Note that Walter wrote the command as "putenv"; I think it is supposed to be "setenv". At least, that's what works for me.]

Subject: Running shell command using system

From: DrDOS

Date: 12 Jan, 2011 03:36:35

Message: 6 of 7

On Jan 11, 5:31 pm, "the cyclist" <thecycl...@gmail.com> wrote:
> "Philipp Steffen" wrote in message <igijv3$2c...@fred.mathworks.com>...
> > Hi Walter, hi Cyclist,
>
> > thanks for your help. I did not know that I can alter the system PATH from within MATLAB. The easiest way for me though is to use the full path in system().
>
> Possibly the best long-term solution is to add the following to your startup.m file, which will set the path at the beginning of each MATLAB session:
>
> %%%%%%%%%%%%%%%%%%%
> % Add directories to the system path (primarily to get "git" to work?)
> setenv('PATH', [getenv('PATH') ':/usr/local/bin'])
> %%%%%%%%%%%%%%%%%%%
>
> [Note that Walter wrote the command as "putenv"; I think it is supposed to be "setenv".  At least, that's what works for me.]

Been looking all night for something that would actually work. I was
having trouble using ghostscript (gs) through the system command on my
macbook. This worked perfectly. Thank you so much.

Subject: Running shell command using system

From: hamid mahmoodi

Date: 17 Feb, 2011 07:59:08

Message: 7 of 7

"Philipp Steffen" wrote in message <ig6r8l$phk$1@fred.mathworks.com>...
> Hi all,
>
> I have the following problem: I would like to run a shell command from within Matlab but I just receive the error "No such file or directory".
>
> I did the following:
> in the MacOS terminal (bash): git --> runs the executable
> in Matlab: system('git') gives the error
>
> If I change the working directory in Matlab to the directory of the executable (/usr/local/git/bin), the following command works !./git.
> Why do I have to change the working directory to the directory of the executable? Is there a way around this?
>

> Thanks in advance,
> Philipp
Hi. I think a better way is use addpath(pwd) command in top of your code:
cd /usr/local/git/bin;
     addpath(pwd);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us