CREATING STAND-ALONE APPLICATIONS
from matlab m-files
A MathWorks utility, mbuild, provides an easy way to specify an options file that can be used to set the
compiler and linker settings, change compilers or compiler settings, switch between C and C++ development,
and build an application. The Compiler (mcc) automatically invokes mbuild under certain conditions. In
particular, mcc -m or mcc –p invokes mbuild to perform compilation and linking. To prevent mcc from
invoking mbuild automatically, the -c option can be used. For example, mcc -mc filename. On systems where
there is exactly one C or C++ compiler, the mbuild utility automatically configures itself for the appropriate
compiler. On systems where there is more than one C or C++ compiler, the mbuild utility lets you select
which of the compilers you want to use. Once a C or C++ compiler is selected, that compiler becomes the
default compiler. The user may later specify another compiler by following the same procedure.
The Compiler, when invoked with the -m macro option, translates input M-files into C source code that is usable in any of the supported executable types. The Compiler also produces the required wrapper file
suitable for a stand-alone application. Then, an ANSI C compiler compiles these C source code files and the
resulting object files are linked against the C/C++ Math Library. Similarly, the Compiler (mcc), when
invoked with the -p macro option, translates input M-files into C++ source code that is usable in any of the
executable types except MEX. The source code for a stand-alone C or C++ application consists either
entirely of M-files or some combination of M-files, MEX-files, and C or C++ source code files. After
compiling the C or C++ source code, the resulting object file is linked with the object libraries.
When you use the Compiler to compile an M-file, it generates C or C++ code, a header file, and a wrapper file. The C or C++ code and the header file are independent of the final target type and target platform. That is, the C or C++ code and header file are identical no matter what the desired final output (i.e.
MEX-functions, stand-alone applications, or libraries). The wrapper file, however, provides the code
necessary to support the output executable type. So, the wrapper file is different for each executable type.
So, the wrapper file is different for each executable type.
Consider the M-file called “PolyVal” shown in Fig. 2.
%***************************************************************
% This program evaluates the polynomial y=x^4 + 2x^3 - x^2 + 4x - 5 at x=5, 6
%***************************************************************
function y=PolyValue(poly,x)
poly=[1 2 -1 4 -5];
x=[5, 6];
y=polyval(poly, x)
Fig. 2 M-file to compute the value of a polynomial
This program evaluates a polynomial at two given points. Typing PolyVal at the MATLAB prompt yields
ans =
865 1711
To create an executable stand-alone file, the mcc command is used at the MATLAB prompt as follows
mcc -m PolyValue
This mcc command generates the following files:
PolyValue.h
PolyValue_main.c
bin
PolyValue.c
PolyValue.exe
PolyValue.h contains the public information.
PolyValue_main.c contain .EXE function interface (wrapper).
bin is a directory containing the MATLAB menu bar and toolbar figure files.
PolyValue.c is the C source code.
PolyValue.exe is the executable application.
Note that when executing the mcc command to link files and libraries, mcc actually calls the mbuild script to perform the functions. To run the stand-alone application, PolyValue, invoke it by typing its name on the MS-DOS command line. However, MATLAB offers a quick way to shell out to DOS by using the bang (!) function. The exclamation point character, “!”, is a shell escape and indicates that the rest of the input line is
a command to the operating system. It is used to invoke utilities or run other programs without quitting from
MATLAB. At the MATLAB prompt type
!PolyValue
The application should run and display the same exact results as before. After quitting the program, the
operating system returns control to MATLAB.
To distribute a stand-alone application, a package containing the following files must be created: (1) The application executable, (2) The contents, if any, of the directory named bin, (3) Any custom MEX-files the application uses, and (4) All the MATLAB Math run-time libraries. The C Math library has prepackaged all the MATLAB run-time libraries required by stand-alone applications into a single, self-extracting archive file, called the Math and Graphics Run-Time Library Installer (mglinstaller.exe). On the PC, the installer is located in C:\matlabR12\extern\lib\win32\. When run, the Installer will extract the libraries from the archive and install them in subdirectories of a directory specified by the user.
How to create an .exe file
after creating the m-file in matlab write the following command in matlab command window
mcc -e filename
Here file name is name of your m file.
It will create a exe file in the current directory of matlab.
from matlab m-files
A MathWorks utility, mbuild, provides an easy way to specify an options file that can be used to set the
compiler and linker settings, change compilers or compiler settings, switch between C and C++ development,
and build an application. The Compiler (mcc) automatically invokes mbuild under certain conditions. In
particular, mcc -m or mcc –p invokes mbuild to perform compilation and linking. To prevent mcc from
invoking mbuild automatically, the -c option can be used. For example, mcc -mc filename. On systems where
there is exactly one C or C++ compiler, the mbuild utility automatically configures itself for the appropriate
compiler. On systems where there is more than one C or C++ compiler, the mbuild utility lets you select
which of the compilers you want to use. Once a C or C++ compiler is selected, that compiler becomes the
default compiler. The user may later specify another compiler by following the same procedure.
The Compiler, when invoked with the -m macro option, translates input M-files into C source code that is usable in any of the supported executable types. The Compiler also produces the required wrapper file
suitable for a stand-alone application. Then, an ANSI C compiler compiles these C source code files and the
resulting object files are linked against the C/C++ Math Library. Similarly, the Compiler (mcc), when
invoked with the -p macro option, translates input M-files into C++ source code that is usable in any of the
executable types except MEX. The source code for a stand-alone C or C++ application consists either
entirely of M-files or some combination of M-files, MEX-files, and C or C++ source code files. After
compiling the C or C++ source code, the resulting object file is linked with the object libraries.
When you use the Compiler to compile an M-file, it generates C or C++ code, a header file, and a wrapper file. The C or C++ code and the header file are independent of the final target type and target platform. That is, the C or C++ code and header file are identical no matter what the desired final output (i.e.
MEX-functions, stand-alone applications, or libraries). The wrapper file, however, provides the code
necessary to support the output executable type. So, the wrapper file is different for each executable type.
So, the wrapper file is different for each executable type.
Consider the M-file called “PolyVal” shown in Fig. 2.
%***************************************************************
% This program evaluates the polynomial y=x^4 + 2x^3 - x^2 + 4x - 5 at x=5, 6
%***************************************************************
function y=PolyValue(poly,x)
poly=[1 2 -1 4 -5];
x=[5, 6];
y=polyval(poly, x)
Fig. 2 M-file to compute the value of a polynomial
This program evaluates a polynomial at two given points. Typing PolyVal at the MATLAB prompt yields
ans =
865 1711
To create an executable stand-alone file, the mcc command is used at the MATLAB prompt as follows
mcc -m PolyValue
This mcc command generates the following files:
PolyValue.h
PolyValue_main.c
bin
PolyValue.c
PolyValue.exe
PolyValue.h contains the public information.
PolyValue_main.c contain .EXE function interface (wrapper).
bin is a directory containing the MATLAB menu bar and toolbar figure files.
PolyValue.c is the C source code.
PolyValue.exe is the executable application.
Note that when executing the mcc command to link files and libraries, mcc actually calls the mbuild script to perform the functions. To run the stand-alone application, PolyValue, invoke it by typing its name on the MS-DOS command line. However, MATLAB offers a quick way to shell out to DOS by using the bang (!) function. The exclamation point character, “!”, is a shell escape and indicates that the rest of the input line is
a command to the operating system. It is used to invoke utilities or run other programs without quitting from
MATLAB. At the MATLAB prompt type
!PolyValue
The application should run and display the same exact results as before. After quitting the program, the
operating system returns control to MATLAB.
To distribute a stand-alone application, a package containing the following files must be created: (1) The application executable, (2) The contents, if any, of the directory named bin, (3) Any custom MEX-files the application uses, and (4) All the MATLAB Math run-time libraries. The C Math library has prepackaged all the MATLAB run-time libraries required by stand-alone applications into a single, self-extracting archive file, called the Math and Graphics Run-Time Library Installer (mglinstaller.exe). On the PC, the installer is located in C:\matlabR12\extern\lib\win32\. When run, the Installer will extract the libraries from the archive and install them in subdirectories of a directory specified by the user.
How to create an .exe file
after creating the m-file in matlab write the following command in matlab command window
mcc -e filename
Here file name is name of your m file.
It will create a exe file in the current directory of matlab.