| Using Simulink | |
During code generation for simulation targets, Embedded MATLAB functions attempt to resolve a called function as a subfunction or function in the Embedded MATLAB run-time library. If the called function is not found in these areas, the function call is resolved as a call to a MATLAB function in the MATLAB path. This applies to function calls of the following types only:
If a function call in an Embedded MATLAB function is resolved as a call to a MATLAB function, you receive a diagnostic warning message when you generate code for the simulation target. For example, if you insert the line
in an Embedded MATLAB function, you receive the following diagnostic warning:
Notice that the warning includes a link to the offending line in the Embedded MATLAB Editor.
Note
If you want to use a MATLAB function instead of its counterpart in the Embedded MATLAB run-time library, call the MATLAB function feval. This function lets you call MATLAB functions indirectly in MATLAB by providing a string name for the MATLAB function and a list of argument values. When you convert existing MATLAB functions and scripts to Embedded MATLAB functions, this feature lets you gradually migrate calls to MATLAB functions to calls to Embedded MATLAB run-time library functions.
|
Generated Code for MATLAB Function Calls
Generated code in a simulation target for a call to a MATLAB function in an Embedded MATLAB function includes only the call to the function. No code is included for the called function itself. This means that the simulation executable you build can execute only on a platform with MATLAB installed.
For Real-Time Workshop and custom targets, calls to MATLAB functions are not permitted. A fatal error message results when you try to build the target, and no code generation takes place at all.
Returning Values from MATLAB Functions
Function calls that are resolved in MATLAB have a return type of mxArray. You can store variables in this return type by assignment. For example, the line
stores a 4-by-4 array of type mxArray in x. You can pass this value to another function resolved in MATLAB as an argument. However, the mxArray type is not defined in Embedded MATLAB functions for operations with other types. For example, the lines
x = zeros(4); //x is a 4-by-4 array of type double y = magic(4); //y is a 4-by-4 array of type mxArray z = x + y; //Error: type mismatch!
receive the following run-time error:
To prevent this error, you must set y to be a 4-by-4 matrix of type double before the assignment is made to the return value from the call magic(4). You can do this by using the Embedded MATLAB run-time library function zeros as follows:
x = zeros(4); //x is a 4-by-4 array of type double y = zeros(4); //y is a 4-by-4 array of type double y = magic(4); //Return from magic converted to type double z = x + y;
In this case, the Embedded MATLAB function knows that the call to the Embedded MATLAB run-time library function zeros with an argument of 4 returns a 4-by-4 matrix of type double, and x is sized accordingly. In the next line, y = zeros(4), y becomes a 4-by-4 matrix of type double in the same way. In the next line, y = magic(4), the return value from the call to the MATLAB function magic is converted at run-time to an array of type double for assignment to y. Finally, the last line, z = x + y, is now homogeneous in type.
In the preceding example, the line y = zeros(4) permanently sets both the type and size of y. This means that if you assign to y the results of magic(5) in the next line by mistake, a fatal size mismatch error results.
![]() | Calling Embedded MATLAB Run-Time Library Functions |
© 1994-2005 The MathWorks, Inc.