Call MATLAB Functions - MATLAB & Simulink
Call MATLAB Functions - MATLAB & Simulink
For unsupported functions other than common visualization functions, you must declare the functions (like p a u s e ) to be extrinsic (see Resolution of Function Calls for Code Generation). Extrinsic functions are not compiled, but instead executed in MATLAB during simulation (see How MATLAB Resolves Extrinsic Functions During Simulation). There are tw o w ays to declare a function to be extrinsic: Use the c o d e r . e x t r i n s i cconstruct in main functions or local functions (see Declaring MATLAB Functions as Extrinsic Functions). Call the function indirectly using f e v a l(see Calling MATLAB Functions Using feval).
1/4/2014
a x i s ( ' e q u a l ' ) ; The code generation softw are detects that a x i sis not supported for code generation and automatically treats it as an extrinsic function. The compiler does not generate code for p a t c hand a x i s , but instead dispatches them to MATLAB for execution. To test the function, follow these steps: 1. Convert p y t h a g o r a sto a MEX function by executing this command at the MATLAB prompt: c o d e g e nr e p o r tp y t h a g o r a sa r g s{ 1 ,1 ,[ . 3. 3. 3 ] } 2. Click the link to the code generation report and then, in the report, view the MATLAB code for c r e a t e _ p l o t . The report highlights the p a t c hand a x i sfunctions to indicate that they are supported only w ithin the MATLAB environment.
3. Run the MEX function by executing this command: p y t h a g o r a s _ m e x ( 3 ,4 ,[ 1 . 00 . 00 . 0 ] ) ; MATLAB displays a plot of the right triangle as a red patch object:
When to Use the coder.extrinsic Construct Use the c o d e r . e x t r i n s i cconstruct to: Call MATLAB functions that do not produce output such as p a u s e during simulation, w ithout generating unnecessary code (see How MATLAB Resolves Extrinsic Functions During Simulation). Make your code self-documenting and easier to debug. You can scan the source code for c o d e r . e x t r i n s i cstatements to isolate calls to MATLAB functions, w hich can potentially create and propagate m x A r r a y s(see Working w ith mxArrays).
http://www.mathworks.com/help/simulink/ug/calling-matlab-functions.html#bq1h2z9-47 2/5
1/4/2014
Save typing. With one c o d e r . e x t r i n s i cstatement, each subsequent function call is extrinsic, as long as the call and the statement are in the same scope (see Scope of Extrinsic Function Declarations). Declare the MATLAB function(s) extrinsic throughout the calling function scope (see Scope of Extrinsic Function Declarations). To narrow the scope, use f e v a l(see Calling MATLAB Functions Using feval). Rules for Extrinsic Function Declarations Observe the follow ing rules w hen declaring functions extrinsic for code generation: Declare the function extrinsic before you call it. Do not use the extrinsic declaration in conditional statements. Scope of Extrinsic Function Declarations The c o d e r . e x t r i n s i cconstruct has function scope. For example, consider the follow ing code: f u n c t i o ny=f o o% # c o d e g e n c o d e r . e x t r i n s i c ( ' r a t ' , ' m i n ' ) ; [ ND ]=r a t ( p i ) ; y=0 ; y=m i n ( N ,D ) ; In this example, r a tand m i nas treated as extrinsic every time they are called in the main function f o o . There are tw o w ays to narrow the scope of an extrinsic declaration inside the main function: Declare the MATLAB function extrinsic in a local function, as in this example: f u n c t i o ny=f o o% # c o d e g e n c o d e r . e x t r i n s i c ( ' r a t ' ) ; [ ND ]=r a t ( p i ) ; y=0 ; y=m y m i n ( N ,D ) ; f u n c t i o ny=m y m i n ( a , b ) c o d e r . e x t r i n s i c ( ' m i n ' ) ; y=m i n ( a , b ) ; Here, the function r a tis extrinsic every time it is called inside the main function f o o , but the function m i nis extrinsic only w hen called inside the local function m y m i n . Call the MATLAB function using f e v a l , as described in Calling MATLAB Functions Using feval.
http://www.mathworks.com/help/simulink/ug/calling-matlab-functions.html#bq1h2z9-47
3/5
1/4/2014
During simulation, MATLAB generates code for the call to an extrinsic function, but does not generate the function's internal code. Therefore, you can run the simulation only on platforms w here you install MATLAB softw are. During code generation, MATLAB attempts to determine w hether the extrinsic function affects the output of the function in w hich it is called for example by returning m x A r r a y sto an output variable (see Working w ith mxArrays). Provided that the output does not change, MATLAB proceeds w ith code generation, but excludes the extrinsic function from the generated code. Otherw ise, MATLAB issues a compiler error.
1/4/2014
F u n c t i o no u t p u t' y 'c a n n o tb eo fM A T L A Bt y p e . To fix this problem, define yto be the type and size of the value that you expect m i nto return in this case, a scalar double as follow s: f u n c t i o ny=f o o% # c o d e g e n c o d e r . e x t r i n s i c ( ' r a t ' ) ; [ ND ]=r a t ( p i ) ; y=0 ;%D e f i n eya sas c a l a ro ft y p ed o u b l e y=m i n ( N , D ) ;
http://www.mathworks.com/help/simulink/ug/calling-matlab-functions.html#bq1h2z9-47
5/5