MATLAB Tutorial Chapter 8 MATLAB compiler The previous chapters have discussed programming within the MATLAB environment It has been noted that MATLAB is an interpreted language meaning that each command is converted to machine level instructions one by one during execution While this allows one to program in both interactive and batch mode the extra overhead required to convert the commands at run time is not desired Also any programs written in MATLAB can only be run on computers that have a copy of MATLAB so portability is limited MATLAB includes an optional compiler to circumvent these problems by converting m files to C or C code and optionally linking this code with its mathematics and graphics libraries to produce a stand alone executable that may be run without the interpretation overhead on any machine with a compatible operating system platform In this section we demonstrate the MATLAB compiler to produce a standalone executable from the simple example of section 6 4 Note that the program containing the main program has been rewritten from the previous script file version since the MATLAB compiler only works with function m files The first file a script file called make file m is executed from the interactive prompt to perform the compilation alternatively the command mcc can be entered manually make file m This MATLAB script m file calls the compiler to convert the MATLAB source code files for make plot trig to C link the object files with the MATLAB graphics library and then produce a stand alone executable Kenneth Beers Massachusetts Institute of Technology Department of Chemical Engineering 7 31 2001 mcc B sgl make plot trig plot trig 1 trig func 1 get input scalar assert scalar make plot trig m main program file make plot trig m This MATLAB m file makes a plot of the general function f x a sin x b cos x for user selected values of a and b Kenneth Beers Massachusetts Institute of Technology Department of Chemical Engineering 7 31 2001 function iflag main make plot trig iflag main 0 signifies no completion disp RUNNING make plot trig disp disp This program produces a plot in 0 2 pi disp of the function disp f x a sin x b cos x disp for user input values of the real scalars a and b disp The following code asks the user to input values of a and b and then uses plot trig to plot trig func 1 by including the function name as an argument in the list prompt Input a check real 1 check sign 0 check int 0 a get input scalar prompt check real check sign check int prompt Input b check real 1 check sign 0 check int 0 b get input scalar prompt check real check sign check int We now call the routine that produces the plot func name trig func 1 plot trig 1 func name a b We now require the user to strike a key before exiting the program pause iflag main 1 return plot trig 1 m function iflag plot trig 1 func name a b iflag 0 signifies no completion First create an x vector from 0 to 2 pi num pts 100 x linspace 0 2 pi num pts Next make a vector of the function values We evaluate the argument function indirectly using the feval command f linspace 0 0 num pts for i 1 num pts f i feval func name x i a b end Then we make the plot figure plot x f xlabel Angle radians ylabel Function value return trig func 1 m function f val trig func 1 x a b f val a sin x b cos x return
View Full Document
Unlocking...