Thread Subject: How to run .exe file from matlab

Subject: How to run .exe file from matlab

From: Zhang

Date: 20 Dec, 2008 05:33:02

Message: 1 of 11

I got a .exe file complied by Fortran. I have to run it thousands of times, each time with different input arguments (which I have to enter manually). I want to do this in matlab automatically. How to call an executable in matlab? How to include the input arguments (for the Fortran exe file) from matlab? I know it is very simple in IDL, since someone just showed me. But I would like to do this in matlab.
Thanks a lot.

Subject: How to run .exe file from matlab

From: us

Date: 20 Dec, 2008 05:45:04

Message: 2 of 11

"Zhang"
> How to call an executable in matlab...

a hint:

     help system;
     help unix;

us

Subject: How to run .exe file from matlab

From: Zhang

Date: 22 Dec, 2008 16:16:02

Message: 3 of 11

This does not solve my problem. When the executable file runs, it needs input data and parameters (which I have to enter manually in matlab command window now). I want this to be done automatically, for example, making 'system' or 'unix' take some input arguments. How to do that? I cannot find any help from matlab help files. Thanks.

"us " <us@neurol.unizh.ch> wrote in message <gii0p0$258$1@fred.mathworks.com>...
> "Zhang"
> > How to call an executable in matlab...
>
> a hint:
>
> help system;
> help unix;
>
> us

Subject: How to run .exe file from matlab

From: Walter Roberson

Date: 22 Dec, 2008 17:11:22

Message: 4 of 11

Zhang wrote:
> This does not solve my problem. When the executable file runs, it needs input data and
> parameters (which I have to enter manually in matlab command window now). I want this to
> be done automatically, for example, making 'system' or 'unix' take some input arguments.

If it is a graphical interface, then you would need to use something like the java "robot"
class.

If it is straight-forward text input (as seems likely from your reference to a simple way
to do it in IDL) then I suggest you create files containing the inputs, and use I/O
redirection:

fname = tempname;
while exist(fname, 'file')
  fname = tempname;
end

fid = fopen(fname, 'wt');
fprintf(fid, .... ) %write out the input parameters the program will need
fclose(fid);

system(['MyExecutable.exe < ' fname]); %run executable with content of fname as inputs

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: How to run .exe file from matlab

From: Zhang

Date: 22 Dec, 2008 21:24:02

Message: 5 of 11

This works for me! Thank you very much, Walter.

Walter Roberson <roberson@hushmail.com> wrote in message <xSP3l.18$H11.3@newsfe09.iad>...
> Zhang wrote:
> > This does not solve my problem. When the executable file runs, it needs input data and
> > parameters (which I have to enter manually in matlab command window now). I want this to
> > be done automatically, for example, making 'system' or 'unix' take some input arguments.
>
> If it is a graphical interface, then you would need to use something like the java "robot"
> class.
>
> If it is straight-forward text input (as seems likely from your reference to a simple way
> to do it in IDL) then I suggest you create files containing the inputs, and use I/O
> redirection:
>
> fname = tempname;
> while exist(fname, 'file')
> fname = tempname;
> end
>
> fid = fopen(fname, 'wt');
> fprintf(fid, .... ) %write out the input parameters the program will need
> fclose(fid);
>
> system(['MyExecutable.exe < ' fname]); %run executable with content of fname as inputs
>
> --
> .signature note: I am now avoiding replying to unclear or ambiguous postings.
> Please review questions before posting them. Be specific. Use examples of what you mean,
> of what you don't mean. Specify boundary conditions, and data classes and value
> relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: How to run .exe file from matlab

From: Thanh Chung

Date: 19 Dec, 2009 15:52:03

Message: 6 of 11

There is a simple way to solve this issue:
[> !filename.xxx
(xxx is filename extension)

"Zhang" <zjiang@aps.anl.gov> wrote in message <gii02e$i22$1@fred.mathworks.com>...
> I got a .exe file complied by Fortran. I have to run it thousands of times, each time with different input arguments (which I have to enter manually). I want to do this in matlab automatically. How to call an executable in matlab? How to include the input arguments (for the Fortran exe file) from matlab? I know it is very simple in IDL, since someone just showed me. But I would like to do this in matlab.
> Thanks a lot.

Subject: How to run .exe file from matlab

From: Ali Culp

Date: 10 May, 2010 17:45:14

Message: 7 of 11

I'm trying to do a really similar thing and am still having trouble!

For the first part of the instructions:
fname=tempname;
while exist(fname,'file')
fname=tempname;
end

What is the purpose of this step? Is tempname the file location: aka C:\My Programs\myprogram.exe? Also, what is 'file'?

I understand the second part using the fprintf function. But then for the system command:

system(['MyExectutable.exe<'fname])

Is this the exact code to use? What does the < indicate?

I've just started using matlab so simple explanations would be greatly appreciated!

Subject: How to run .exe file from matlab

From: Walter Roberson

Date: 10 May, 2010 19:05:38

Message: 8 of 11

Ali Culp wrote:
> I'm trying to do a really similar thing and am still having trouble!
>
> For the first part of the instructions:
> fname=tempname;
> while exist(fname,'file')
> fname=tempname;
> end
>
> What is the purpose of this step? Is tempname the file location: aka
> C:\My Programs\myprogram.exe? Also, what is 'file'?

'file' is a literal string. When passed as the second argument to exist(), it
causes exist() to check whether the file indicated by the first argument exists.

tempname() is a function that each time returns a different filename in the
system temporary directory. Although it tries to return a file name that is
not already in use, there is the possibility that between the time tempname()
ran and the time of the existence test that the file was created, so this is
an extra layer of protection. It is, however, not a secure protection, because
the file might come into existence after it is checked for but before the
program actually asks to open the file, so it is more a protection against the
possibility that tempname has a glitch and accidentally returned the name of a
file that is already in use.


> I understand the second part using the fprintf function. But then for
> the system command:
>
> system(['MyExectutable.exe<'fname])
> Is this the exact code to use? What does the < indicate?

You should replace 'MyExecutable.exe' with a string that is the name of your
executable.

The '<' tells system() that you want the standard input for the executable to
come from the following file name (the value of fname).

Subject: How to run .exe file from matlab

From: Rops ropss

Date: 25 May, 2010 13:55:08

Message: 9 of 11

What if I already have a file with a specified format(.dat in my case), that the executable needs to run? How to do this then?

system('C:\.....\anyexecutable.exe');

here, anyexecutable.exe needs a specified file I have(data1234.dat)..........

how to input this file into the command in MATLAB??





> 'file' is a literal string. When passed as the second argument to exist(), it
> causes exist() to check whether the file indicated by the first argument exists.
>
> tempname() is a function that each time returns a different filename in the
> system temporary directory. Although it tries to return a file name that is
> not already in use, there is the possibility that between the time tempname()
> ran and the time of the existence test that the file was created, so this is
> an extra layer of protection. It is, however, not a secure protection, because
> the file might come into existence after it is checked for but before the
> program actually asks to open the file, so it is more a protection against the
> possibility that tempname has a glitch and accidentally returned the name of a
> file that is already in use.
>
>
> > I understand the second part using the fprintf function. But then for
> > the system command:
> >
> > system(['MyExectutable.exe<'fname])
> > Is this the exact code to use? What does the < indicate?
>
> You should replace 'MyExecutable.exe' with a string that is the name of your
> executable.
>
> The '<' tells system() that you want the standard input for the executable to
> come from the following file name (the value of fname).

Subject: How to run .exe file from matlab

From: Walter Roberson

Date: 25 May, 2010 14:33:53

Message: 10 of 11

Rops ropss wrote:
> What if I already have a file with a specified format(.dat in my case),
> that the executable needs to run? How to do this then?
>
> system('C:\.....\anyexecutable.exe');
>
> here, anyexecutable.exe needs a specified file I
> have(data1234.dat)..........
>
> how to input this file into the command in MATLAB??

You do it exactly like I already said.

fname = 'data1234.dat';
system(['C:\.....\anyexecutable.exe<', fname]);

If, that is, the data file is to be available on standard input. If the
data file _name_ has to be provided to the executable as an argument,
then change the '<' to a space.

Subject: How to run .exe file from matlab

From: Rops ropss

Date: 26 May, 2010 13:46:05

Message: 11 of 11

Thanks a lot.....
It werked.












Walter Roberson <roberson@hushmail.com> wrote in message <ldRKn.10570$%u7.501@newsfe14.iad>...
> Rops ropss wrote:
> > What if I already have a file with a specified format(.dat in my case),
> > that the executable needs to run? How to do this then?
> >
> > system('C:\.....\anyexecutable.exe');
> >
> > here, anyexecutable.exe needs a specified file I
> > have(data1234.dat)..........
> >
> > how to input this file into the command in MATLAB??
>
> You do it exactly like I already said.
>
> fname = 'data1234.dat';
> system(['C:\.....\anyexecutable.exe<', fname]);
>
> If, that is, the data file is to be available on standard input. If the
> data file _name_ has to be provided to the executable as an argument,
> then change the '<' to a space.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
exe Nikos Makris 1 Nov, 2011 05:34:31
executable syst... Zhang 22 Dec, 2008 11:20:20
reference us 20 Dec, 2008 00:50:05
system us 20 Dec, 2008 00:50:05
unix us 20 Dec, 2008 00:50:05
executable Zhang 20 Dec, 2008 00:35:04
rssFeed for this Thread

Contact us at files@mathworks.com