Professional Documents
Culture Documents
EXE in m-file
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. 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" <giaccariluigi@msn.com> wrote in message <h6cesa$mm6$1@fred.mathworks.com>... > 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" <b.luong@fogale.findmycountry> wrote in message <h6ckur$ll$1@fred.mathworks.com>... > "Luigi Giaccari" <giaccariluigi@msn.com> wrote in message <h6cesa$mm6$1@fred.mathworks.com>... > > 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" <giaccariluigi@msn.com> wrote in message <h6dj4f$3aa$1@fred.mathworks.com>... > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h6ckur$ll$1@fred.mathworks.com>... > > "Luigi Giaccari" <giaccariluigi@msn.com> wrote in message <h6cesa$mm6$1@fred.mathworks.com>... > > > 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. >
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
MATLAB
MATLAB
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.
History
Cleve 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]
Syntax
The 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/matrices
As 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;
Semicolons
Unlike 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).
Graphics
Function 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)
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})')
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:
Structures
MATLAB 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 handles
MATLAB supports elements of lambda-calculus by introducing function handles, or function references, which are implemented either in .m files or anonymous/nested functions.
Secondary programming
MATLAB also carries secondary programming which incorporates the MATLAB standard code into a more user friendly way to represent a function or system.
Classes
Although 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.
Object-oriented programming
MATLAB'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!
License
MATLAB 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.
Alternatives
See 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 history
Version[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
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)
ABSTRACT
MATLAB 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.
INTRODUCTION
MATLAB 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.
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 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 WORKSTATIONS
The 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
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 <RET> 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.
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.
************************** 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.
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).
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 COMMANDS
The 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.
To convert an M-file into C and create the corresponding MEX file that runs at the MATLAB prompt: mcc x mainfn sub1fn sub2fn
Creating Libraries
To 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 ...
CONCLUSION
MATLAB 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.
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 " <namo1983@hotmail.com> wrote in message news:idigt5$o28$1@fred.mathworks.com... > 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 slord@mathworks.com 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.com
Tags for this Thread
Bottom of Form
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 Tutorial
Cyclismo.org More Tutorials: Front Page vectors matrices vector operations loops plots executable files subroutines if statements data files
Executable Files
In 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
Class double double double double double double array array array array array array
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
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.
0 comments Tags
.exe , gui
Products
4 answers
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
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 comment
Maryam Emad on 24 Dec 2011 Pleas explain more !
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 comment
Walter 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
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.
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" <cssmwbs@xxxxxxxxx> 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: <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/deployment_process6.html
http://www.mathworks.com/matlabcentral/answers/4788-libraries
Top of Form
libraries
Natalia asked on 5 Apr 2011 Latest activity: Answer by Kaustubha Govind on 5 Apr 2011 Accepted Answer from Kaustubha Govind Hello! I continue to suffer with the included files and libraries. I can't understand, why there is a following error:
>> mex -IC:\OgreSDK\samples\include -IC:\OgreSDK\include -LC:\OgreSDK\lib OgreAppSource.cpp -l OgreMain_d.lib -l OIS_d.lib Warning: MEX could not find the library "" specified with -l option. MEX looked for a file with one of the names: .lib lib.lib MEX looked for the library in the following directories: C:\Program Files\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB C:\Program Files\Microsoft Visual Studio 9.0\VC\LIB C:\Program Files\\Microsoft SDKs\Windows\v6.0A\lib C:\Program Files\Microsoft Visual Studio 9.0\SDK\v2.0\lib C:\Program Files\MATLAB\R2010b\extern\lib\win32 C:\Program Files\SQLXML 4.0\bin\ C:\Program Files\MATLAB\R2010b\extern\lib\win32\microsoft
C:\OgreSDK\lib Warning: MEX could not find the library "" specified with -l option. MEX looked for a file with one of the names: .lib lib.lib MEX looked for the library in the following directories: C:\Program Files\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB C:\Program Files\Microsoft Visual Studio 9.0\VC\LIB C:\Program Files\\Microsoft SDKs\Windows\v6.0A\lib C:\Program Files\Microsoft Visual Studio 9.0\SDK\v2.0\lib C:\Program Files\MATLAB\R2010b\extern\lib\win32 C:\Program Files\SQLXML 4.0\bin\ C:\Program Files\MATLAB\R2010b\extern\lib\win32\microsoft C:\OgreSDK\lib LINK : fatal error LNK1181: cannot open input file 'OgreMain_d.lib' C:\PROGRA~1\MATLAB\R2010B\BIN\MEX.PL: Error: Link of 'OgreAppSource.mexw32' failed. ??? Error using ==> mex at 208 Unable to complete successfully.
0 comments Tags
matlab compiler
Products
MATLAB
2 answers
Kaustubha Govind answered on 5 Apr 2011 Accepted answer I'm sorry, I think I made a mistake in my answer to your previous question - there should be no blankspace between -l and your library name. Also, you don't need to add the .lib extension since MEX does that automatically for you. Try:
>> mex -IC:\OgreSDK\samples\include -IC:\OgreSDK\include -LC:\OgreSDK\lib OgreAppSource.cpp -lOgreMain_d -lOIS_d
3 comments
Natalia on 5 Apr 2011
I am running the mex command from the directory where is my source file OgreAppSource.cpp. Thank you, Kaustubha, but I have faced a new problem >> mex -IC:\OgreSDK\samples\include -IC:\OgreSDK\include -LC:\OgreSDK\lib OgreAppSource.cpp -lOgreMain_d -lOIS_d LINK : error LNK2001: unresolved external symbol mexFunction C:\Users\98AF~1\AppData\Local\Temp\mex_C4W8i8\templib.x : fatal error LNK1120: 1 unresolved externals C:\PROGRA~1\MATLAB\R2010B\BIN\MEX.PL: Error: Link of 'OgreAppSource.mexw32' failed. ??? Error using ==> mex at 208 Unable to complete successfully. The code(which I try to make mex)has no problems in visual studio 2008. If it is necessary, I am ready to lay out source codes... Very,very much grateful Natalia Kaustubha Govind on 6 Apr 2011 Okay, so it appears that OgreAppSource.cpp is not really a MEX-function (remember that a MEX-function has not main(), but a mexFunction()instead) - I am assuming that you build an executable from this file in Visual Studio, which is why it works. But MEX actually produces a DLL-like binary, and needs a different interface. You will need to modify your source code and create a MEX wrapper around it. Natalia on 6 Apr 2011 Thank you, many, many :)
Arnaud Miege answered on 5 Apr 2011 The error message seems to suggest that mex can't find the file OgreMain_d.lib. Are you sure this file is on your MATLAB path or in your current directory?
3 comments
Natalia on 5 Apr 2011 Yes, I am sure. The file 'OgreMain_d.lib' is in a folder C:\OgreSDK\lib. I have checked up names. Arnaud Miege on 5 Apr 2011
OK, but from which directory are you running the mex command? Shouldn't you have: >> mex -IC:\OgreSDK\samples\include -IC:\OgreSDK\include -LC:\OgreSDK\lib OgreAppSource.cpp -l C:\OgreSDK\lib\OgreMain_d.lib -l OIS_d.lib
Sandy I am very thankfull to your suggestion, but this installer is in order of 80MB! I just cant keep sending 80MB of file to everyone for just some kB of my orginal program. I mean, cant i just find the required dll's and just distribute them? Please let me know if there is way to copy the required dll's and just distribute them only. Thank you
From: "Bryan Smith" <cssmwbs@xxxxxxxxx> Date: Tue, 7 Feb 2006 00:56:51 -0500
bryan unfortunately, if you want to continue using matlab to write your code, you'll have to distribute it with the entire 80 mb installer. and if you think the 80 meg thing is big, it decompresses into nearly 300 mb on the end user's machine. serious bloat. this has been addressed in other posts on this newsgroup, and as far as i know, there is not yet a solution. sorry i cannot help more.
sandy Thank you very much for your help, but I always wonder why each version of matlab is so incompatible with one another. Anyways, I may have to distribute the 80mb file i guess, Thank you for your help. http://newsgroups.derkeiler.com/Archive/Comp/comp.soft-sys.matlab/2006-02/msg01425.html\