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:
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 19

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 19

"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 19

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 19

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 19

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 19

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 19

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 19

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 19

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 19

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 19

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.

Subject: How to run .exe file from matlab

From: Hema

Date: 3 Jan, 2013 09:04:10

Message: 12 of 19

Hi walter,

I am runing an executable from matlab, but I have more than one file as input file. What will be the solution in that case?
fname1 = dat1234.dat';
fname2 = dat1234.dat';
fname 3= dat1234.dat';

system(['C:\......\anyexecutable.exe <', fname1,fname2,fname3]);

I shall apreciate any help with this.

Thanks
Hema

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.

Subject: How to run .exe file from matlab

From: Steven_Lord

Date: 3 Jan, 2013 14:38:51

Message: 13 of 19



"Hema " <hema.wadhwa@intecsea.com> wrote in message
news:kc3hia$gja$1@newscl01ah.mathworks.com...
> Hi walter,
>
> I am runing an executable from matlab, but I have more than one file as
> input file. What will be the solution in that case?
> fname1 = dat1234.dat';
> fname2 = dat1234.dat';
> fname 3= dat1234.dat';
>
> system(['C:\......\anyexecutable.exe <', fname1,fname2,fname3]);
>
> I shall apreciate any help with this.

The easiest way is to have your function accept an arbitrary number of
string inputs and LOAD or use your custom file format reader function to
read in the data from the files whose names you pass into the executable.

function anyexecutable(varargin)
mydata = cell(1, nargin);
for whichdata = 1:nargin
    mydata{whichdata} = load(varargin{whichdata});
end

Call this as:

anyexecutable dat1234.dat dat2345.dat dat3456.dat

Now the data from the third file whose name you specified is stored in
mydata{3}

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to run .exe file from matlab

From: E U

Date: 10 Jan, 2013 04:13:17

Message: 14 of 19

I have similar problem. I have a simple fortran code (test.exe) which takes exponential of a number. I want to run this code several times from matlab as follows: open an input.dat file, write a number into it, call test.exe and then write the output to output.dat file.

The associated matlab code is as follows:


   for n=1:10
    
    input_val=rand(1,1); %creating a random number

          
    fid_input = fopen('input.dat','w');
    fprintf(fid_input, '%0.6f \n',input_val); % putting the number into input file
    fclose(fid_input);

   
    system ('test.exe') ;
  
     
    fid_y = fopen('output.dat','r');
    output_val=fscanf(fid_y,'%f'); % reading the output value from the output file
    fclose(fid_y);
   
  end

When I compile the fortran code on a 32 bit machine and then run the above matlab code on 32 bit matlab installed on 32 bit machine it works. However, if I compile the fortran code on a 64 bit machine and then run the above matlab code on 64 bit matlab installed on 64 bit machine it does not work! What can be the reason?
Any help is appreciated.

Subject: How to run .exe file from matlab

From: E U

Date: 10 Jan, 2013 04:13:17

Message: 15 of 19

I have similar problem. I have a simple fortran code (test.exe) which takes exponential of a number. I want to run this code several times from matlab as follows: open an input.dat file, write a number into it, call test.exe and then write the output to output.dat file.

The associated matlab code is as follows:


   for n=1:10
    
    input_val=rand(1,1); %creating a random number

          
    fid_input = fopen('input.dat','w');
    fprintf(fid_input, '%0.6f \n',input_val); % putting the number into input file
    fclose(fid_input);

   
    system ('test.exe') ;
  
     
    fid_y = fopen('output.dat','r');
    output_val=fscanf(fid_y,'%f'); % reading the output value from the output file
    fclose(fid_y);
   
  end

When I compile the fortran code on a 32 bit machine and then run the above matlab code on 32 bit matlab installed on 32 bit machine it works. However, if I compile the fortran code on a 64 bit machine and then run the above matlab code on 64 bit matlab installed on 64 bit machine it does not work! What can be the reason?
Any help is appreciated.

Subject: How to run .exe file from matlab

From: Steven_Lord

Date: 10 Jan, 2013 14:18:34

Message: 16 of 19



"E U" <uzun_el@yahoo.com> wrote in message
news:kclf4t$243$1@newscl01ah.mathworks.com...
> I have similar problem. I have a simple fortran code (test.exe) which
> takes exponential of a number. I want to run this code several times from
> matlab as follows: open an input.dat file, write a number into it, call
> test.exe and then write the output to output.dat file.
>
> The associated matlab code is as follows:

*snip*

> When I compile the fortran code on a 32 bit machine and then run the above
> matlab code on 32 bit matlab installed on 32 bit machine it works.
> However, if I compile the fortran code on a 64 bit machine and then run
> the above matlab code on 64 bit matlab installed on 64 bit machine it does
> not work! What can be the reason?

Define "does not work" -- does it throw an error? Does it issue a warning?
Does it give you a different answer than you expect? Does it crash MATLAB or
your machine?

Post that information to the group and someone may be able to offer a
suggestion; determining the cause of this problem may require posting a
SMALL section of your Fortran code as well, but let's start with the answer
to the question above.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to run .exe file from matlab

From: Engin Uzunel

Date: 10 Jan, 2013 15:49:08

Message: 17 of 19

What I mean by "does not work" is that
''output_val'' is empty (zero by zero). That means that the
 system ('test.exe') is not working.
The same code was working when the fortran code was compiled on a 32 bit machine and its exe is called on 32 bit matlab installed on that machine.

"Steven_Lord" <slord@mathworks.com> wrote in message <kcmijq$114$1@newscl01ah.mathworks.com>...
>
>
> "E U" <uzun_el@yahoo.com> wrote in message
> news:kclf4t$243$1@newscl01ah.mathworks.com...
> > I have similar problem. I have a simple fortran code (test.exe) which
> > takes exponential of a number. I want to run this code several times from
> > matlab as follows: open an input.dat file, write a number into it, call
> > test.exe and then write the output to output.dat file.
> >
> > The associated matlab code is as follows:
>
> *snip*
>
> > When I compile the fortran code on a 32 bit machine and then run the above
> > matlab code on 32 bit matlab installed on 32 bit machine it works.
> > However, if I compile the fortran code on a 64 bit machine and then run
> > the above matlab code on 64 bit matlab installed on 64 bit machine it does
> > not work! What can be the reason?
>
> Define "does not work" -- does it throw an error? Does it issue a warning?
> Does it give you a different answer than you expect? Does it crash MATLAB or
> your machine?
>
> Post that information to the group and someone may be able to offer a
> suggestion; determining the cause of this problem may require posting a
> SMALL section of your Fortran code as well, but let's start with the answer
> to the question above.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: How to run .exe file from matlab

From: Engin Uzunel

Date: 11 Jan, 2013 08:04:09

Message: 18 of 19

Even the exe of a simple ''hello world'' fortran code does not work using the matlab ''system'' command on a 64 bit machine.


"Engin Uzunel" <uzun_el@yahoo.com> wrote in message <kcmntk$lq5$1@newscl01ah.mathworks.com>...
> What I mean by "does not work" is that
> ''output_val'' is empty (zero by zero). That means that the
> system ('test.exe') is not working.
> The same code was working when the fortran code was compiled on a 32 bit machine and its exe is called on 32 bit matlab installed on that machine.
>
> "Steven_Lord" <slord@mathworks.com> wrote in message <kcmijq$114$1@newscl01ah.mathworks.com>...
> >
> >
> > "E U" <uzun_el@yahoo.com> wrote in message
> > news:kclf4t$243$1@newscl01ah.mathworks.com...
> > > I have similar problem. I have a simple fortran code (test.exe) which
> > > takes exponential of a number. I want to run this code several times from
> > > matlab as follows: open an input.dat file, write a number into it, call
> > > test.exe and then write the output to output.dat file.
> > >
> > > The associated matlab code is as follows:
> >
> > *snip*
> >
> > > When I compile the fortran code on a 32 bit machine and then run the above
> > > matlab code on 32 bit matlab installed on 32 bit machine it works.
> > > However, if I compile the fortran code on a 64 bit machine and then run
> > > the above matlab code on 64 bit matlab installed on 64 bit machine it does
> > > not work! What can be the reason?
> >
> > Define "does not work" -- does it throw an error? Does it issue a warning?
> > Does it give you a different answer than you expect? Does it crash MATLAB or
> > your machine?
> >
> > Post that information to the group and someone may be able to offer a
> > suggestion; determining the cause of this problem may require posting a
> > SMALL section of your Fortran code as well, but let's start with the answer
> > to the question above.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com

Subject: How to run .exe file from matlab

From: Steven_Lord

Date: 11 Jan, 2013 15:03:08

Message: 19 of 19



"Engin Uzunel" <uzun_el@yahoo.com> wrote in message
news:kcoh1p$ff1$1@newscl01ah.mathworks.com...
> Even the exe of a simple ''hello world'' fortran code does not work using
> the matlab ''system'' command on a 64 bit machine.

Call SYSTEM with two outputs. The first should be a nonzero value and the
second should contain output from the system indicating why calling your
"hello world" executable didn't work. If that doesn't provide you with
enough information to determine the cause, respond in the group with those
outputs.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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