Got Questions? Get Answers.
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 External Programs

Subject: Running External Programs

From: CFD

Date: 15 Aug, 2008 07:25:03

Message: 1 of 4

I would like to execute an external program. The following
method works fine, when I have one file to examine
(Mesh_Airfoil_1.jou):

!gambit.exe -inp Mesh_Airfoil_1.jou

However, I have a system of 80 files which must be
indepedently executed by GAMBIT through MATLAB. Is there a
way to automate this process through the bang character
(!), instead of copying and pasting the shell command 80
times one after the other for the different files?

Thanks

Subject: Running External Programs

From: Rune Allnor

Date: 15 Aug, 2008 07:45:23

Message: 2 of 4

On 15 Aug, 09:25, "CFD " <manas.khur...@rmit.edu.au> wrote:
> I would like to execute an external program. The following
> method works fine, when I have one file to examine
> (Mesh_Airfoil_1.jou):
>
> !gambit.exe -inp Mesh_Airfoil_1.jou
>
> However, I have a system of 80 files which must be
> indepedently executed by GAMBIT through MATLAB. Is there a
> way to automate this process through the bang character
> (!), instead of copying and pasting the shell command 80
> times one after the other for the different files?

There is a way, but using the EVAL function as I do below is
generally considered dangerous:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
myProgram = 'gambit.exe';

for n=1:N
   filename = ['Mesh_Airfoil_;,int2str(n),'.jou'];
   myCommand = ['!',myProgram,' -inp ',filename];

   disp(myCommand) % Just to check that the command is correct
   %eval(myCommand); % Execute the command
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

This loops over N files, assembles the command line for each file
and then *displays* the command lines for you to check.

If you are happy that these command lines are correct, you can
uncomment the last line inside the loop, and the commands will
be run as well the next time you run the script.

Rune

Subject: Running External Programs

From: Titus

Date: 15 Aug, 2008 07:59:44

Message: 3 of 4


"Rune Allnor" <allnor@tele.ntnu.no> schrieb im Newsbeitrag
news:77958e33-a787-4a87-ba8b-bf14a0387c93@e53g2000hsa.googlegroups.com...
> On 15 Aug, 09:25, "CFD " <manas.khur...@rmit.edu.au> wrote:
>> I would like to execute an external program. The following
>> method works fine, when I have one file to examine
>> (Mesh_Airfoil_1.jou):
>>
>> !gambit.exe -inp Mesh_Airfoil_1.jou
>>
>> However, I have a system of 80 files which must be
>> indepedently executed by GAMBIT through MATLAB. Is there a
>> way to automate this process through the bang character
>> (!), instead of copying and pasting the shell command 80
>> times one after the other for the different files?
>
> There is a way, but using the EVAL function as I do below is
> generally considered dangerous:
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> myProgram = 'gambit.exe';
>
> for n=1:N
> filename = ['Mesh_Airfoil_;,int2str(n),'.jou'];
> myCommand = ['!',myProgram,' -inp ',filename];
>
> disp(myCommand) % Just to check that the command is correct
> %eval(myCommand); % Execute the command
> end
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> This loops over N files, assembles the command line for each file
> and then *displays* the command lines for you to check.
>
> If you are happy that these command lines are correct, you can
> uncomment the last line inside the loop, and the commands will
> be run as well the next time you run the script.
>
> Rune

Hi Rune,
an alternative to the bang (and eval) is using system:

myCommand = [myProgram ' -inp ' filename];
system(myCommand);

Titus

Subject: Running External Programs

From: CFD

Date: 15 Aug, 2008 08:21:02

Message: 4 of 4

"Titus" <titus.edelhofer@mathworks.de> wrote in message
<g83d1g$jqk$1@fred.mathworks.com>...
>
> "Rune Allnor" <allnor@tele.ntnu.no> schrieb im
Newsbeitrag
> news:77958e33-a787-4a87-ba8b-
bf14a0387c93@e53g2000hsa.googlegroups.com...
> > On 15 Aug, 09:25, "CFD " <manas.khur...@rmit.edu.au>
wrote:
> >> I would like to execute an external program. The
following
> >> method works fine, when I have one file to examine
> >> (Mesh_Airfoil_1.jou):
> >>
> >> !gambit.exe -inp Mesh_Airfoil_1.jou
> >>
> >> However, I have a system of 80 files which must be
> >> indepedently executed by GAMBIT through MATLAB. Is
there a
> >> way to automate this process through the bang
character
> >> (!), instead of copying and pasting the shell command
80
> >> times one after the other for the different files?
> >
> > There is a way, but using the EVAL function as I do
below is
> > generally considered dangerous:
> >
> > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%
> > myProgram = 'gambit.exe';
> >
> > for n=1:N
> > filename = ['Mesh_Airfoil_;,int2str(n),'.jou'];
> > myCommand = ['!',myProgram,' -inp ',filename];
> >
> > disp(myCommand) % Just to check that the command
is correct
> > %eval(myCommand); % Execute the command
> > end
> > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%
> >
> > This loops over N files, assembles the command line
for each file
> > and then *displays* the command lines for you to check.
> >
> > If you are happy that these command lines are correct,
you can
> > uncomment the last line inside the loop, and the
commands will
> > be run as well the next time you run the script.
> >
> > Rune
>
> Hi Rune,
> an alternative to the bang (and eval) is using system:
>
> myCommand = [myProgram ' -inp ' filename];
> system(myCommand);
>
> Titus
>
>
A big thank-you to both Rune and Titus. The code works
fine based on your advice! Thanks again!

Tags for this Thread

No tags are associated with 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