Matlab Exe

Embed Size (px)

Citation preview

Thread Subject: Using .EXE in m-fileI am using a .exe file do to some computation. The problem I have is that launching the.exe file eith the ! command do not stop the m-code script exexution. SO the the script continues to run without waiting for the .exe to finish. Is there a way to comunicate in the script the end of the execution of an exe? Or maybe some C++ command to comunicate the script to wait for the exe? http://www.advancedmcode.org/

"Luigi Giaccari" wrote in message ... > I am using a .exe file do to some computation. The problem I have is that launching the.exe file eith the ! command do not stop the m-code script exexution. > SO the the script continues to run without waiting for the .exe to finish. If don't add the "&" sign it should wait until the command finishes. Try this two commands (then type something, then Ctrl C to terminate) If your exe returns right the way then I guess because it launches an independent process on its own.

"Bruno Luong" wrote in message ... > "Luigi Giaccari" wrote in message ... > > I am using a .exe file do to some computation. The problem I have is that launching the.exe file eith the ! command do not stop the m-code script exexution. > > SO the the script continues to run without waiting for the .exe to finish. >> > > Luigi, > > If don't add the "&" sign it should wait until the command finishes. Try this two commands

(then type something, then Ctrl C to terminate) > > >> !more > >> !more & > > If your exe returns right the way then I guess because it launches an independent process on its own. > > Bruno Thank you Bruno, You are right I was using !program.exe& Removing the & it works, it is easier than I thought. Thank you

Hello, I am facing problem in making matlab wait for an exe file to finish process. I am using ! command to call the exe file. I will need an output file file from this process so that I can use it in the matlab script to carry on further process. This is the command im using to call the 'fluent' .exe !'C:\Fluent.Inc\n tbin\win64\fluent.exe -r6 .3.26 3ddp -i "fluent2.jou"' What should I do for the matlab to wait? Thanks, Priyanka "Luigi Giaccari" wrote in message ... > "Bruno Luong" wrote in message ... > > "Luigi Giaccari" wrote in message ... > > > I am using a .exe file do to some computation. The problem I have is that launching the.exe file eith the ! command do not stop the m-code script exexution. > > > SO the the script continues to run without waiting for the .exe to finish.

>>> >> > > Luigi, >> > > If don't add the "&" sign it should wait until the command finishes. Try this two commands (then type something, then Ctrl C to terminate) >> > > >> !more > > >> !more & >> > > If your exe returns right the way then I guess because it launches an independent process on its own. >> > > Bruno > > Thank you Bruno, You are right > > I was using > > !program.exe& > > Removing the & it works, it is easier than I thought. >

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.

Creating exe in Matlab without Matlab compilerExpand Post Hi, I want to create a simple exe(Without any supporting files...just a standalone exe) We dont have Matlab compiler I read in web that we can create exe in Matlab6.0,6.1 using C++ compiler. I have Matlab 7.1 and above. With the current Matlab version can we use C++ or C compilers for creating exe? Treat it as very urgent and provide me a solution

Re: Creating exe in Matlab without Matlab compilerIn order to create a executable, you need Matlab Compiler. http://www.mathworks.com/products/compiler/ I think this might help in creating an exe from c++ http://www.onecore.net/howtocreate-s...atlab-file.htm Last edited by hammerhead; Mar 3rd, 2008 at 5:23 am. which compiler has mathlab ????? we have is it obsolete ? ^ http://en.wikipedia.org/wiki/Matlab 0 Hi, Can you please tell me is it possible to create just a standalone exe. currently when i am creating exe with command mcc -m jk.m i am getting the exe with bin folder containing .fig files. I just want exe only so that i can call it from a web application for streaming thanks in advance The only method I know is by purchasing the Matlab compiler. If any other methods exist sorry I am not aware of them. Isn't exe atomatically created in the folder you work when the program is automatically compiled ?? Matlab does not work that way. There are files that are similar to exe called mat files. But they require matlab installed and cannot be transported to another PC without matlab. In order to

create a standalone file, one has to purchase a license of Matlab compiler that is a differnet product. why do yuo need a mat file when you have and .exe ?? You dont have an exe. Matlab does not allow one to build an exe. Its not like conventional programming.Bottom of Form

MATLABMATLAB

MATLAB R2011a screenshot Developer(s) MathWorks R2011b / September 1, 2011; 4 Stable release months ago Written in C, Java Operating system Cross-platform[1] Type Technical computing License Proprietary Website MATLAB product page

multi-paradigm: imperative, procedural, object-oriented, array Appeared in late 1970s Designed by Cleve Moler Developer MathWorks Typing discipline dynamic, weak OS Cross-platform Usual filename .m extensions Paradigm(s) MATLAB (matrix laboratory) is a numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, and Fortran. Although MATLAB is intended primarily for numerical computing, an optional toolbox uses the MuPAD symbolic engine, allowing access to symbolic computing capabilities. An additional package, Simulink, adds graphical multi-domain simulation and Model-Based Design for dynamic and embedded systems. In 2004, MATLAB had around one million users across industry and academia.[2] MATLAB users come from various backgrounds of engineering, science, and economics. MATLAB is widely used in academic and research institutions as well as industrial enterprises.

HistoryCleve Moler, the chairman of the computer-science department at the University of New Mexico, started developing MATLAB in the late 1970s.[3] He designed it to give his students access to LINPACK and EISPACK without them having to learn Fortran. It soon spread to other universities and found a strong audience within the applied mathematics community. Jack Little, an engineer, was exposed to it during a visit Moler made to Stanford University in 1983. Recognizing its commercial potential, he joined with Moler and Steve Bangert. They rewrote MATLAB in C and founded MathWorks in 1984 to continue its development. These rewritten libraries were known as JACKPAC.[4] In 2000, MATLAB was rewritten to use a newer set of libraries for matrix manipulation, LAPACK.[5] MATLAB was first adopted by researchers and practitioners in control engineering, Little's specialty, but quickly spread to many other domains. It is now also used in education, in particular the teaching of linear algebra and numerical analysis, and is popular amongst scientists involved in image processing.[3]

SyntaxThe MATLAB application is built around the MATLAB language. The simplest way to execute MATLAB code is to type it in the Command Window, which is one of the elements of the MATLAB Desktop. When code is entered in the Command Window, MATLAB can be used as an interactive mathematical shell. Sequences of commands can be saved in a text file, typically using the MATLAB Editor, as a script or encapsulated into a function, extending the commands available.[6]

Variables

Variables are defined as the assignment operator, =. MATLAB is a weakly dynamically typed programming language. It is a weakly typed language because types are implicitly converted.[7] It is a dynamically typed language because variables can be assigned without declaring their type, except if they are to be treated as symbolic objects,[8] and that their type can change. Values can come from constants, from computation involving values of other variables, or from the output of a function. For example:-->> x = 17 x = 17 >> x = 'hat' x = hat >> y = x + 0 y = 104 97 >> x = [3*4, pi/2] x = 12.0000 1.5708 >> y = 3*sin(x) y = -1.6097 3.0000

116

Vectors/matricesAs suggested by its name (a contraction of "Matrix Laboratory"), MATLAB can create and manipulate arrays of 1 (vectors), 2 (matrices), or more dimensions. In the MATLAB vernacular, a vector refers to a one dimensional (1N or N1) matrix, commonly referred to as an array in other programming languages. A matrix generally refers to a 2-dimensional array, i.e. an mn array where m and n are greater than 1. Arrays with more than two dimensions are referred to as multidimensional arrays. Arrays are a fundamental type and many standard functions natively support array operations allowing work on arrays without explicit loops. Therefore the MATLAB language is also an example of array programming language. A simple array is defined using the syntax: init:increment:terminator. For instance:>> array = 1:2:9 array = 1 3 5 7 9

defines a variable named array (or assigns a new value to an existing variable with the name array) which is an array consisting of the values 1, 3, 5, 7, and 9. That is, the array starts at 1 (the init value), increments with each step from the previous value by 2 (the increment value), and stops once it reaches (or to avoid exceeding) 9 (the terminator value).>> array = 1:3:9 array = 1 4 7

the increment value can actually be left out of this syntax (along with one of the colons), to use a default value of 1.>> ari = 1:5 ari = 1 2 3 4 5

assigns to the variable named ari an array with the values 1, 2, 3, 4, and 5, since the default value of 1 is used as the incrementer.

Indexing is one-based,[9] which is the usual convention for matrices in mathematics, although not for some programming languages. Matrices can be defined by separating the elements of a row with blank space or comma and using a semicolon to terminate each row. The list of elements should be surrounded by square brackets: []. Parentheses: () are used to access elements and subarrays (they are also used to denote a function argument list).>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] A = 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 >> A(2,3) ans = 11

Sets of indices can be specified by expressions such as "2:4", which evaluates to [2, 3, 4]. For example, a submatrix taken from rows 2 through 4 and columns 3 through 4 can be written as:>> A(2:4,3:4) ans = 11 8 7 12 14 1

A square identity matrix of size n can be generated using the function eye, and matrices of any size with zeros or ones can be generated with the functions zeros and ones, respectively.>> eye(3) ans = 1 0 0 0 1 0 0 0 1 >> zeros(2,3) ans = 0 0 0 0 0 0 >> ones(2,3) ans = 1 1 1 1 1 1

Most MATLAB functions can accept matrices and will apply themselves to each element. For example, mod(2*J,n) will multiply every element in "J" by 2, and then reduce each element modulo "n". MATLAB does include standard "for" and "while" loops, but (as in other similar applications such as R), using the vectorized notation often produces code that is faster to execute. This code, excerpted from the function magic.m, creates a magic square M for odd values of n (MATLAB function meshgrid is used here to generate square matrices I and J containing 1:n).[J,I] = meshgrid(1:n); A = mod(I+J-(n+3)/2,n); B = mod(I+2*J-2,n); M = n*A + B + 1;

SemicolonsUnlike many other languages, where the semicolon is used to terminate commands, in MATLAB the semicolon serves to suppress the output of the line that it concludes (it serves a similar purpose in Mathematica). Commands that have return values would be: numbers/vectors/matrices and various mathematical functions executed with these (addition, multiplication etc.), strings and strings functions etc. Each number/vector/matrix/string/function with a return value, if it appears in a line not terminated by a semicolon, will have its value displayed on the screen once the line is interpreted. Some Matlab commands (such as the graphical "plot" command), however, will not have any return value associated with them, in which case the semicolon is redundant in that sense. However, a semicolon, as it symbolizes the end of a command, can allow another command to be listed in the same line. That will hardly be used at all in a Matlab program file (i.e., a Matlab file with extension ".m") which typically groups several commands, each in a separate line - for better "readability"; but it could be handy when typing commands in the command line prompt window in the desktop, whereby each line is interpreted and executed immediately after the pressing of the enter key (and therefore the semicolon allows to type a command and not execute it before typing another one).

GraphicsFunction plot can be used to produce a graph from two vectors x and y. The code:x = 0:pi/100:2*pi; y = sin(x); plot(x,y)

produces the following figure of the sine function:

Three-dimensional graphics can be produced using the functions surf, plot3 or mesh.[X,Y] = meshgrid(-10:0.25:10,10:0.25:10); f = sinc(sqrt((X/pi).^2+(Y/pi).^2)); mesh(X,Y,f); axis([-10 10 -10 10 -0.3 1]) xlabel('{\bfx}') ylabel('{\bfy}') [X,Y] = meshgrid(-10:0.25:10,10:0.25:10); f = sinc(sqrt((X/pi).^2+(Y/pi).^2)); surf(X,Y,f); axis([-10 10 -10 10 -0.3 1]) xlabel('{\bfx}') ylabel('{\bfy}')

zlabel('{\bfsinc} ({\bfR})') hidden off

zlabel('{\bfsinc} ({\bfR})')

This code produces a wireframe 3D plot of the two-dimensional unnormalized sinc function:

This code produces a surface 3D plot of the two-dimensional unnormalized sinc function:

StructuresMATLAB supports structure data types. Since all variables in MATLAB are arrays, a more adequate name is "structure array", where each element of the array has the same field names. In addition, MATLAB supports dynamic field names (field look-ups by name, field manipulations etc). Unfortunately, MATLAB JIT does not support MATLAB structures, therefore just a simple bundling of various variables into a structure will come at a cost.[citation needed]

Function handlesMATLAB supports elements of lambda-calculus by introducing function handles, or function references, which are implemented either in .m files or anonymous/nested functions.

Secondary programmingMATLAB also carries secondary programming which incorporates the MATLAB standard code into a more user friendly way to represent a function or system.

ClassesAlthough MATLAB supports classes, the syntax and calling conventions are significantly different from other languages. MATLAB supports value classes and reference classes, depending if the class has handle as super-class (for reference classes) or not (for value classes). Depending if a class is declared as value or reference, method call behavior is different. For example, a call to a method

object.method();

can alter any member of object only if object is an instance of a reference class.

Graphical user interface programmingMATLAB supports developing applications with graphical user interface features.

Object-oriented programmingMATLAB's support for object-oriented programming includes classes, inheritance, virtual dispatch, packages, pass-by-value semantics, and pass-by-reference semantics.[10]classdef hello methods function doit(this) disp('Hello!') end end end

When put into a file named hello.m, this can be executed with the following commands:>> x = hello; >> x.doit; Hello!

Interfacing with other languagesMATLAB can call functions and subroutines written in the C programming language or Fortran. A wrapper function is created allowing MATLAB data types to be passed and returned. The dynamically loadable object files created by compiling such functions are termed "MEX-files" (for MATLAB executable).[11][12] Libraries written in Java, ActiveX or .NET can be directly called from MATLAB and many MATLAB libraries (for example XML or SQL support) are implemented as wrappers around Java or ActiveX libraries. Calling MATLAB from Java is more complicated, but can be done with MATLAB extension,[13] which is sold separately by MathWorks, or using an undocumented mechanism called JMI (Java-to-Matlab Interface),[14] which should not be confused with the unrelated Java Metadata Interface that is also called JMI. As alternatives to the MuPAD based Symbolic Math Toolbox available from MathWorks, MATLAB can be connected to Maple or Mathematica.[15] Libraries also exist to import and export MathML.[16]

LicenseMATLAB is a proprietary product of MathWorks, so users are subject to vendor lock-in.[2][17] Although MATLAB Builder can deploy MATLAB functions as library files which can be used with .NET or Java application building environment, future development will still be tied to the MATLAB language. Each toolbox is purchased separately. If an evaluation licence is requested, the MathWorks sales department requires detailed information about the project for which MATLAB is to be evaluated. Overall the process of acquiring a licence is expensive in terms of money and time[citation needed]. If at all granted (which happens often), the evaluation licence is valid for two to four weeks. A student version of MATLAB is also available.

AlternativesSee also: list of numerical analysis software and comparison of numerical analysis software MATLAB has a number of competitors.[18] Commercial competitors include Mathematica, Maple, IDL by ITT Visual Information Solutions and Metlynx. There are also free open source alternatives to MATLAB, in particular GNU Octave, FreeMat, and Scilab which are intended to be mostly compatible with the MATLAB language (but not the MATLAB desktop environment). Among other languages that treat arrays as basic entities (array programming languages) are APL and J, Fortran 95 and 2003, as well as the statistical language S (the main implementations of S are S-PLUS and the popular open source language R). There are also several libraries to add similar functionality to existing languages, such as Itpp for C++, Perl Data Language for Perl, ScalaLab for Scala and SciPy together with NumPy and Matplotlib for Python.

Release historyVersion[19] MATLAB 1.0 MATLAB 2 MATLAB 3 MATLAB 3.5 MATLAB 4 MATLAB 4.2c MATLAB 5.0 MATLAB 5.1 MATLAB 5.1.1 MATLAB 5.2 MATLAB 5.2.1 MATLAB 5.3 MATLAB 5.3.1 MATLAB 6.0 MATLAB R7 R8 R9 1997 R9.1 R10 1998 R10.1 R11 1999 R11.1 R12 R12.1 January, 1999 November, 1999 November, 2000 2000 2001 June, 2001 March, 1998 Release name Year Release Date 1984 1986 1987 1990 1992 1994 1996 December, 1996 May, 1997 Ran on Windows 3.1. Required a math coprocessor Ran on MS-DOS but required at least a 386 processor. Version 3.5m required math coprocessor Notes

6.1 MATLAB 6.5 MATLAB 6.5.1 MATLAB 6.5.2 MATLAB 7 MATLAB 7.0.1 MATLAB 7.0.4 MATLAB 7.1 MATLAB 7.2 MATLAB 7.3 MATLAB 7.4 MATLAB 7.5 MATLAB 7.6 MATLAB 7.7 MATLAB 7.8 MATLAB 7.9 MATLAB 7.9.1 MATLAB 7.10 MATLAB 7.11 MATLAB 7.11.1 MATLAB 7.12 MATLAB 7.13

R13 R13SP1

2002 July, 2002 2003

R13SP2 R14 R14SP1 R14SP2 2005 R14SP3 R2006a 2006 R2006b R2007a 2007 R2007b R2008a 2008 R2008b R2009a 2009 R2009b R2009bSP1 R2010a R2010b R2010bSP1 R2011a R2011b 2004 June, 2004 October, 2004 March 7, 2005 September 1, 2005 March 1, 2006 September 1, HDF5-based MAT-file support 2006 March 1, 2007 Last release for Windows 2000 and PowerPC September 1, Mac. License Server support for Windows 2007 Vista[20] March 1, 2008 October 9, 2008 March 6, 2009 First release for 32-bit & 64-bit Windows 7. September 4, First release for Intel 64-bit Mac, and last for 2009 Solaris SPARC. April 1, 2010 2010 March 5, 2010 Last release for Intel 32-bit Mac. September 3, 2010 March 17, 2011 2011 April 8, 2011 September 1, 2011

File Extensions

Native.fig MATLAB Figure .m MATLAB function, script, or class .mat MATLAB binary file for storing variables .mex... MATLAB executable (platform specific, e.g. ".mexmac" for the Mac, ".mexglx" for Linux, etc.) .p MATLAB content-obscured .m file (result of pcode() )

Third-party.jkt GPU Cache file generated by Jacket for MATLAB (AccelerEyes) .mum MATLAB CAPE-OPEN Unit Operation Model File (AmsterCHEM)

ABSTRACTMATLAB has been recognized as the premier program for numerical computations and data visualization. Due to the fact that MATLAB is an interpreted language, M-files execute slower than compiled programs written in other languages, such as C, C++, and Fortran. Furthermore, M-files require the presence of MATLAB to run. Recently, The MathWorks introduced a set of tools to automatically convert M-files into executable programs that will run independently of MATLAB. This paper states the important features of the Compiler, describes the conversion process, and presents examples that illustrate the steps involved in creating stand-alone applications. Test results that compare execution times of M-files and executable programs are also presented.

INTRODUCTIONMATLAB is a powerful language that simplifies the process of solving technical problems in a variety of disciplines by including a very extensive library of predefined functions. It also offers many special-purpose tool boxes that address specific areas, such as signal processing and control systems, and provides Graphical User Interface (GUI) tools that make it suitable for application development. Because of these capabilities, MATLAB has been recognized as the premier program for numerical computations and data visualization and has been used by the educational, industrial, and government communities all over the world. Due to the fact that MATLAB is an interpreted language, MATLAB files (M-files) execute slower than compiled programs written in other languages, such as C, C++, and Fortran. In addition, M-files require the presence of MATLAB to run. To address these weaknesses, The MathWorks recently introduced a set of tools that automatically convert M-files into MEX files and C/C++ source code, which can then be compiled to produce stand-alone applications. Stand-

alone applications are executable programs that can run even if MATLAB is not installed on the system. The tools required for generating stand-alone applications are: MATLAB, the MATLAB Compiler, the MATLAB C/C++ Math Library, and a C/C++ compiler. The MATLAB C/C++ Graphics Library is also required to create stand-alone applications that make use of Handle Graphics functions. The process of converting M-files to C/C++ files and creating stand-alone applications is described in three documents totaling over 1000 pages. This paper describes the important features of the Compiler and its associated tools, states the benefits of converting M-files to MEX and stand-alone applications, and presents examples that illustrate the steps involved in the conversion process.

THE MATLAB COMPILERThe MathWorks uses the phrase MATLAB Compiler, with a capital letter C, to refer to the product that translates M-files to C or C++ source code [1]. They also use the word "compiler" with a lowercase c to refer to the C or C++ compiler. The MATLAB Compiler version 2.1 may be used to take M-files as input and produce the following outputs:

C source code for building MEX-files C or C++ source code for combining with other modules to form stand-alone applications. C code S-functions for use with Simulink C shared libraries and C++ static libraries

MEX-files run in the same workspace as the MATLAB interpreter, however, stand-alone C/C++ applications run independently of MATLAB. There are four main reasons to compile M-files: (1) To speed up the execution of programs: Compiled C or C++ code typically runs faster than its M-file equivalent, (2) To create stand-alone applications: These applications take advantage of the mathematical functions of MATLAB but do not require its presence to run, (3) To create C shared libraries or C++ static libraries: The Compiler may be used to create a C shared library (dynamically linked libraries, or DLLs, on Microsoft Windows 95/98/2000/NT) or a C++ static library from an algorithm developed in MATLAB. The algorithm may then be integrated into a C/C++ application, and (4) To hide proprietary algorithms: M-files are ASCII text files that anyone can view and modify. MEX-files or stand-alone applications can not be viewed and modified. The current Compiler supports most of the functionality of MATLAB, however, it cannot compile script M-files, function M-files that call a script, and function M-files that use objects.

THE MATLAB C/C++ GRAPHICS LIBRARYThe C/C++ Graphics Library is a collection of graphics routines that make the MATLAB plotting and visualization capabilities available to stand-alone C and C++ applications [4]. The library contains more than 100 routines, including MATLAB 6.0 built-in graphics functions, such as surf, plot, get, and set, and some commonly used M-file graphics functions, such as newplot, gcf, gca, gco, and gcbf. Using the graphics library with the Compiler and the C/C++ Math Library makes it possible to compile M-files that include lines, text, meshes, and polygons, as well as graphical user interface (GUI) components such as menus, push buttons, and dialog boxes.

The C/C++ Graphics Library does not support some MATLAB features, including MATLAB objects, MATLAB Java objects, plotedit command, and propedit command. In addition to these restrictions, the graphics library provides limited support for certain callback coding practices. Furthermore, the graphics library supports only a subset of print command switches to specify device drivers. For example, the graphics library supports most of the MATLAB built-in drivers, such as the PostScript drivers, but it does not support any of the Ghostscript drivers. The C/C++ Graphics Library is available for these platforms: PCs running Microsoft Windows or Linux, Sun, HP, SGI, and Compaq Alpha UNIX platforms. The process of creating stand-alone applications is similar for both PC and UNIX machines. To create a stand-alone graphics application, the Compiler is used along with the Graphics Library bundle file. Bundle files are ASCII text files that contain Compiler command line options and arguments. For example, to compile an M-file called test1.m into a C application, the following command is entered the MATLAB prompt. mcc -B sgl test1 This command invokes the Compiler, using the -B flag to specify the bundle file used to create C stand-alone graphics applications, sgl. The first time the Compiler is run to create a stand-alone graphics application, it creates a subdirectory, named \bin, in the current working directory. The Compiler puts in these directory versions of the MATLAB menu bar and toolbar figure files that are used by stand-alone graphics applications at run-time. The command for a C++ application is mcc B sglcpp fname. UNIX WORKSTATIONS The Compiler supports the following UNIX Compilers: The GNU C compiler, gcc, (except on HP and SGI64) The system's native ANSI C compiler on all UNIX platforms The system's native C++ compiler on all UNIX platforms (except Linux) The GNU C++ compiler, g++, on Linux.

There are several restrictions regarding the use of supported compilers, including [4]: (1) The SGI C compiler does not handle denormalized floating-point values correctly. Denormalized floating-point numbers are numbers that are greater than 0 and less than the value of DBL_MIN in the compiler's float.h file; (2) Due to a limitation of the GNU C++ compiler (g++) on Linux, try...catch...end blocks do not work; (3) The -A debugline: on option does not work on the GNU C++ compiler (g++) on Linux because it uses try...catch...end.

PC WORKSTATIONSThe Compiler supports the following ANSI C and C++ PC Compilers: Lcc C version 2.4 (included with MATLAB) Watcom C/C++ versions 10.6 and 11.0 Borland C++ versions 5.0, 5.2, 5.3, 5.4, and 5.5

Microsoft Visual C++ (MSVC) versions 5.0 and 6.0

The MATLAB installer should add the following path to the Windows operating system environment: SET PATH=%PATH%;C:\matlabr12\bin\win32. Selecting a C/C++ compiler is accomplished by typing mbuild -setup then following the instructions. Although the Compiler supports the creation of stand-alone C++ applications, it does not support the creation of C++ MEX-files, on both UNIX and PC machines.

CREATING MEX FILESCreating MEX-files on PC or UNIX workstations is a similar process. The examples presented here were tested on a PC workstation running Windows. The Compiler (mcc), when invoked with the -x macro option, translates M code to C code, generates a MEX wrapper, and invokes the mex utility, which builds the C MEX-file source into a MEX-file by linking the MEX-file with the MEX version of the math libraries (libmatlbmx). The MATLAB interpreter dynamically loads MEX-files as they are needed. Most MEX-files run significantly faster than their M-file equivalents. Consider the M-file called PolyRoots shown in Fig. 1, which finds the roots of polynomial y=x^2 + x + 1. %************************************* ************ % This program finds the roots of polynomial y=x^2 + x + 1 * %************************************* ************ function PolyRoots roots([1 1 1]) Fig. 1 M-file to find the roots of a polynomial Typing PolyRoots at the MATLAB prompt will run the program and produce: ans = -0.5000 + 0.8660i -0.5000 - 0.8660i To create a MEX-file, the mcc command is used at the MATLAB prompt as follows. mcc x PolyRoots This mcc command generates the following files: PolyRoots.dll PolyRoots_mex.c PolyRoots.h roots.h PolyRoots.c PolyRoots.c contains MEX-file C source code PolyRoots.h contains the public information

PolyRoots_mex.c containing the MEX-function interface (MEX wrapper) PolyRoots.dll is the dynamic link library file of the the executable MEX-file Roots.h is a header file that contains function prototype for roots.m mcc automatically invokes mex to create PolyRoots.dll from PolyRoots.c and PolyRoots_mex.c. The mex utility encapsulates the appropriate C compiler and linker options for the system. The MEX version of the file is invoked from the MATLAB interpreter the same way an M-file version is invoked. Typing PolyRoots at the MATLAB prompt yields the same exact results as before. Given an M-file and a MEX-file with the same root name in the same directory, the MEX-file takes precedence. MATLAB runs the MEX-file version rather than the M-file version.

CREATING STAND-ALONE APPLICATIONSA 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. 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.

INPUTTING FROM THE KEYBOARD AND READING DATA FILESIt is often necessary to create interactive programs that allow the user to enter input from the keyboard. Furthermore, in many applications, the input must come from previously generated data files stored in binary or MATLAB format. Consider the M-file program of Fig. 3. This program, ReadDataFile, loads input data signal from a mat-file then calls a function, DispImg, to display the image of the data, signal. %************************************* ********************* % This programs loads data from an input file and displays the image * %************************************* ********************* fname = input('Enter input file name (without extension [.mat]): ', 's'); tic %load input data from matlab file *.mat disp('STARTING: Load input data ...'); eval(['load ' fname]); cuttail=0; %as defined in function Dispimg dispimg(signal,'LIN',cuttail,0); %call dispimg toc %stop timer Fig. 3 Loading a data file and displaying image This program can be run at the MATLAB prompt by typing ReadDataFile as follows >>ReadDataFile >>Enter input file name (without extension [.mat]): test3 STARTING: Load input data ... mean and standar deviation of real are 0.012599 0.51493 mean and standar deviation of imag are -0.012585 0.50625 magnitude min and max are 0 2.8454 threshold is 2.8454 display image in LIN scale elapsed_time = 37.6800 In addition to the above output, the program produces a graph representing the image of the input data signal as shown in the following figure.

As stated earlier, the Compiler does not support the INPUT function to manipulate workspace environment. Therefore, the Compiler can not correctly compile the program of Fig. 3. To get around this restriction, one can manually edit the C code generated by the Compiler. This approach, however, is inefficient and not practical. The C/C++ Graphics Library offers an input dialog box MATLAB function, INPUTDLG, which can be used to automatically solve this problem. The above example may be modified as shown in Fig. 5. The modified program consists of a main function, ReadDataFile2, which calls two functions, LoadInput and DispImg. Creating a stand-alone application by writing the source code in more than one M-file is very practical. This approach allows the programmer to take advantage of MATLAB's interpretive development environment. After getting the M-file version of the program to work properly, the code is compiled and converted into a stand-alone application.

%************************************* ********************* % This programs loads data from an input file and displays the image * %************************************* ********************* function ReadDataFile2 % prompt for input file name prompt={'Enter input file name (without extension [.mat]): '}; tic %start timer title2='Input File name'; lines=1; defaultfn={'test250'}; fname1 = inputdlg(prompt, title2, lines, defaultfn); fname=fname1{1} %extracting the first element in the array % signal=loadinput(fname); %call loadinput to read file cuttail=0; %as defined in function Dispimg dispimg(signal,'LIN',cuttail,0); %call dispimg toc %stop timer Fig. 5 Loading a data file and displaying image We show in Fig. 6 the function that reads the input data file as requested by ReadDataFile2, LoadInput. %************************************* %This function reads an input data file (.mat) * %************************************* function signal=loadinput(fname1) % Reading input signal load(fname1, 'signal'); Fig. 6 Function to read an input data file Since ReadDataFile2 uses a graphics library function, the graphics library must be included when compiling the file as follows mcc -B sgl ReadDataFile2 dispimg loadinput This command creates an executable file, ReadDataFile2.exe, which can be run at the MATLAB prompt by typing !ReadDataFile2 Running the program produces the following input box

Instead of the default test250, the user can type in a desired file name. After clicking on OK, the program produces the same exact results as before (except a different execution time).

CREATING AND TESTING A STAND-ALONE REAL APPLICATIONThe examples presented so far require little time to execute. To illustrate the benefits of the Compiler and its associated libraries, a program that implements the Range Migration Algorithm for Synthetic Aperture Radar (SAR) image formation was used. The program, which is not shown here, consists of a main function (rma8c) and six subfunctions. The executable code is produced by typing mcc -B sgl rma8c loadinput dispimg localmax stolt_int2 resol3db invpaint The program was run at the MATLAB prompt by typing !rma8c To compare the execution speed of M-files, MEX files, and EXE files, rma8c was run on a 1GHz machine with these three types. The upper part of Table 1 summarizes the results. Table 1 Comparison of execution times Execution time (seconds) Machine 1 GHz PC, 128 MB RAM 1 GHz PC, 128 MB RAM File Rma8c Run # M-File 1 2 Rma8cs 1 2 734.24 739.15 688.55 690.31 MEX-file .EXE C 397.43 405.51 339.99 338.67 328.40 331.26 306.48 305.39 Ratio (.EXE/M-file 0.447 0.447 0.445 0.442

A shorter version of Rma8c, Rma8cs, was created by removing code that generates several intermediate graphs. Rma8cs was run on the same machine and the results are shown in the lower part of Table 1.

SUMMARY OF COMMANDSThe following summarizes how to use the MATLAB Compiler to generate some of the most common results. The commands apply to both UNIX and PC machines. Note that you can add the -g option to any of these commands for debugging purposes.

Converting from MATLAB to MEX Files

To convert an M-file into C and create the corresponding MEX file that runs at the MATLAB prompt: mcc x mainfn sub1fn sub2fn

Converting from MATLAB to EXE Files (C compiler)To convert an M-file into C and to create a corresponding stand-alone executable file that can be run without MATLAB (at the DOS prompt), use: mcc m mainfn sub1fn sub2fn To convert an M-file that contains Handle Graphics functions into C and to create a corresponding stand-alone executable file that can be run without MATLAB (at the DOS prompt), use: mcc B sgl mainfn sub1fn sub2fn

Converting from MATLAB to EXE Files (C++ compiler)To convert an M-file into C++ and to create a corresponding stand-alone executable file that can be run without MATLAB (at the DOS prompt), use: mcc p mainfn sub1fn sub2fn To convert an M-file that contains Handle Graphics functions into C++ and to create a corresponding stand-alone executable file that can be run without MATLAB (at the DOS prompt), use: mcc B sglcpp mainfn sub1fn sub2fn

Converting from MATLAB to C CodeTo produce a C file from an M-file (and functions), use: mcc mc mainfn sub1fn sub2fn To produce a EXE file from a C file, use: mbuild mainfn sub1fn sub2fn

Creating LibrariesTo create a C library, use mcc -m -W lib:libfoo -T link:lib foo.m. To create a C++ library, use: mcc -p -W lib:libfoo -T compile:lib foo.m. To create a C shared library that performs specialized calculations that you can call from your own programs, use: mcc -W lib:mylib -L C -t -T link:lib -h Function1 Function2 ...

CONCLUSIONMATLAB files execute slower than compiled programs written in other languages, such as C, C++, and Fortran. In addition, M-files require the presence of MATLAB to run. The MathWorks recently introduced a set of tools that automatically convert M-files into C/C++ source code, which can then be compiled to produce stand-alone applications. This paper discusses the major features of the Compiler suite, describes the benefits of compiling M-files, and presents examples that illustrate the steps involved in the conversion process. Test results show that execution time of .EXE files is less than 50% of that of M-files. While the Compiler offers an easy way to convert M-files to stand-alone applications, it has some major limitations.

Thread Subject: how to change .m file to independent .exe fileSubject: how to change .m file to independent .exe file From: Namo Date: 6 Dec, 2010 11:20:05 Message: 1 of 2 Reply to this message Add author to My Watch List View original format Flag as spam I know that I can change m file to .exe file but the file need Matlab compiler to be installed in the other machine. Is there a way to create independent exe file from .m file even if it is larger than usual but does't need the compiler

Subject: how to change .m file to independent .exe file From: Steven_Lord Date: 6 Dec, 2010 14:40:06 Message: 2 of 2 Reply to this message Add author to My Watch List View original format Flag as spam

"Namo " wrote in message news:[email protected]... > I know that I can change m file to .exe file but the file need Matlab > compiler to be installed in the other machine. No, it requires the MCR to be installed. That's different from requiring MATLAB Compiler to be installed. > Is there a way to create independent exe file from .m file even if it is

> larger than usual but does't need the compiler You could try using Embedded MATLAB, but that would put some additional limitations on your code. http://www.mathworks.com/help/toolbox/eml/eml_product_page.html -Steve Lord [email protected] comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.comTags for this Thread

Add a New Tag:Top of Form

Bottom of Form

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. Feed for this Thread Contact us at [email protected]

1994-2012 The MathWorks, Inc. Site Help Patents Trademarks Privacy Policy Preventing Piracy Terms of Use RSS Facebook Twitter Featured MathWorks.com Topics:

New Products Support Documentation Training Webinars Newsletters MATLAB Trials Careers

Matlab TutorialCyclismo.org More Tutorials: Front Page vectors matrices vector operations loops plots executable files subroutines if statements data files

Executable FilesIn this tutorial we will assume that you know how to create vectors and matrices, know how to index into them, and know about loops. For more information on those topics see one of our tutorials on vectors, matrices, vector operations, loops, or plotting. In this tutorial we will introduce the basic operations for creating executable files. Once you have a general routine in a matlab file, it allows you to perform more complex operations, and it is easier to repeat these operations. For example, you might have a set of instructions to use Euler's approximation for a differential equation (see the tutorial on loops), but you want to be able to use those instructions for different equations. As an example, a simple file to approximate the D.E. y'= 1/y using Euler's method is found. To execute the commands in the file, the step size and the initial value must be specified. Once done, you can easily approximate the given D.E. for a wide variey of initial conditions and step sizes. First, you will need to create the file. The easiest editor on our system is to just use the built in matlab editor. It will allow you to do some very simple file manipulations. The editor is very simple and easy to start. It is not a very advanced editor, though.

Matlab executable files (called M-files) must have the extension ".m". In this example a file called simpleEuler.m is created. To get Matlab to execute the commands in the file simply type in "simpleEuler". Matlab will then search the current directory for the file "simpleEuler", read the file, and execute the commands in the file. If matlab cannot find the file you will get an error message:??? Undefined function or variable 'simpleEuler'.

If this is the case then either you mistyped the name of the program, the program is misnamed, or the file is located in directory that matlab does not know about. In the later case you have to let matlab know which directory to search. The list of directories that is searched for files is called the "path." For more information on how to set the path there are two articles at the mathworks site that go into more detail: text command and graphical. If you are not familiar with a more advanced editor use matlab's built in editor to create the file. Type in the following command at the matlab prompt:>> edit simpleEuler.m

Once the editor appears on the screen either type or cut and paste the necessary matlab commands:% % % % % % % % % % % % % % % % % % % % % % % % file: simpleEuler.m This matlab file will find the approximation to dy/dx = 1/y y(0) = starty To run this file you will first need to specify the step the following: h : the step size starty : the initial value The routine will generate three vectors. The first vector is x which is the grid points starting at x0=0 and have a step size h. The second vector is an approximation to the specified D.E. The third vector is the true solution to the D.E. If you haven't guessed, you cna use the percent sign to add comments.

x = [0:h:1]; y = 0*x;

y(1) = starty; for i=2:max(size(y)), y(i) = y(i-1) + h/y(i-1); end true = sqrt(2*x+1);

Once the commands are in place, save the file. Go back to your original window and start up matlab. The file is called up by simply typing in the base name (in this case simpleEuler).>> simpleEuler ??? Undefined function or variable h. Error in ==> /home/black/math/mat/examples/simpleEuler.m On line 28 ==> x = [0:h:1];

If you try to call the file without first defining the variables h and starty, you will get an error message. You must first specify all of the variables that are not defined in the file itself.>> >> >> >> h = 1/16; starty = 1; simpleEuler whos Name Size h i starty true x y 1x1 1x1 1x1 1x17 1x17 1x17

Bytes 8 8 8 136 136 136

Class double double double double double double array array array array array array

Grand total is 54 elements using 432 bytes >> plot(x,y,'rx',x,true)

Once the necessary variables are defined, and you type in the command simpleEuler, matlab searched the current directory for a file called simpleEuler.m. Once it found the file, it read the file and executed the commands as if you had typed them from the keyboard. If you would like to run the program again with a different step size, you have to be careful. The program will write over the vectors x,y, and true. If you want to save these vectors, you must do so explicitly!>> >> >> >> >> >> x0 = x; y0 = y; true0 = true; h = h/2; simpleEuler whos Name Size h i 1x1 1x1

Bytes 8 8

Class double array double array

starty true true0 x x0 y y0

1x1 1x33 1x17 1x33 1x17 1x33 1x17

8 264 136 264 136 264 136

double double double double double double double

array array array array array array array

Grand total is 153 elements using 1224 bytes >> plot(x0,abs(true0-y0),'gx',x,abs(true-y),'yo');

Now you have two approximations. The first is with a step size of 1/16, and it is stored in the vectors x0 and y0. The second approximation is for a step size of 1/32 and is found in the vectors x and y.The previous tutorial is an introduction to simple plotting in matlab. The next tutorial is an introduction to subroutines in matlab.

Go back to the main page. This tutorial written by Kelly Black. It is licensed under a Creative Commons AttributionShareAlike 2.5 License.

Top of Form Bottom of Form

Save GUI as executable fileMaryam Emad asked on 24 Dec 2011 Latest activity: Answer by Image Analyst on 24 Dec 2011 Accepted Answer from Walter Roberson I made a GUI and saved the .m and .fig file in the work directory. thus I wrote the command [mcc -mv filename.m] in the command window. It generates a filename.exe file. this file is executable. But it doesnot run in a machine where matlab has not setup. Now i want to make an application that will run in any in any machine independent of Matlab is it possible? Other thing , My GUI contain some button in order to load '*.dat 'file. All things are good except this button , its show an error to load this file !

0 comments Tags

.exe , gui

Products

4 answers

Top of Form Bottom of Form

Walter Roberson answered on 24 Dec 2011

Accepted answer It is not possible to create an application that will run in any machine independent of MATLAB. All that is supported is creating applications that can run under MS Windows XP or later, or under Linux, or under Mac OS X. Different versions are required for Windows 32, Windows 64, Linux, and OS X. There is no Universal Binary.

0 comments

Top of Form Bottom of Form

Fangjun Jiang answered on 24 Dec 2011 You have to install the MCR on that machine that doesn't have the MATLAB. The easiest way to do it is to run deploytool. It helps you to package all the files that are needed for deployment. doc deploytool

1 commentMaryam Emad on 24 Dec 2011 Pleas explain more !

Top of Form Bottom of Form

Image Analyst answered on 24 Dec 2011 See the FAQ: http://matlab.wikia.com/wiki/FAQ#My_standalone_executable_won.27t_run_on_the_target_co mputer._What_can_I_try.3F Your files don't load because your executable is not your real executable - it's really a self extracting program that unloads your real executable to some other folder and runs it from there. Your files are NOT there and so it doesn't find them. You can verify this fact by putting this command in your startup code;fprintf('ctfroot = %s\n', ctfroot);

Then look in the console window to see what folder got printed out. ctfroot is where your real executable is and you'll see it's not where you thought it was. You probably shipped your data

files and put them in the place where the executable is, but like I said, that's not the real executable. To fix the situation, make sure you set your folder to where you know your files live, then construct the file name and check for its existence like this example:folder = fullfile(matlabroot, '\toolbox\images\imdemos'); % or wherever baseFileName = 'peppers.png'; fullFileName = fullfile(folder, baseFileName); % Get the full filename, with path prepended. fullFileName = fullfile(folder, baseFileName); if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it. fullFileName = baseFileName; % No path this time. if ~exist(fullFileName, 'file') % Still didn't find it. Alert user. errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; end end

1 commentWalter Roberson on 24 Dec 2011 Also, you can use the -a option (or use deploytool) to bundle files that will be extracted into ctfroot; then you can use ctfroot as the location to extract them from.Top of Form Bottom of Form

Jan Stolarek answered on 24 Dec 2011 Regarding the problems with loading file: what error exactly do you get? Perhaps there is an error in path to the file?

http://newsgroups.derkeiler.com/Archive/Comp/comp.soft-sys.matlab/200602/msg00227.html

Standalone .exe file independent of Matlab Sandy I want to create an .exe file which is independent of Matlab and which can run on any pc. I have tried using mcc comand, but it creates a matlab dependent .exe file. It will be really helpful if any one posts a step by step procedure to do that. Thank you

From: sandeep.kamalakar@xxxxxxxxx Date: 1 Feb 2006 11:45:18 -0800

From: Jeroen Date: Wed, 01 Feb 2006 21:42:07 +0100

Jeroen If you make an .exe with the Matlab compiler and you want to run it on a PC which does not have Matlab installed, then you always have to install the Matlab runtime libraries. See the help files.

From: "sandy" Date: 3 Feb 2006 10:00:55 -0800

Sandy How exactly can i include these libraries, Can you please explain in steps. I am not able to figure it out by looking at help files. Thank you

From: "Bryan Smith" Date: Fri, 3 Feb 2006 13:31:15 -0500

Jeroen, there is, more or less, a step by step guide in the documentation... perhaps you just did not know where to look: