Model Composer provides direct support for MATLAB through the MCode block. The MCode block applies input values to an M-function for evaluation using AMD's fixed-point data type. The evaluation occurs one time every sample period. The block is capable of keeping internal states with the use of persistent state variables. The input arrguments of the specified M-function determine the input ports of the block. The output arguments of the M-function determine the output ports of the block. The block provides a convenient way to build finite state machines, control logic, and computation heavy systems.
You must write an M-function to construct an MCode block. The M-file must be in the directory of the model file that uses the M-file or in a directory in the MATLAB path.
The following text provides examples that use the MCode block:
- Example 1 Simple Selector shows how to implement a function that returns the maximum value of its inputs;
- Example 2 Simple Arithmetic Operations shows how to implement simple arithmetic operations;
- Example 3 Complex Multiplier with Latency shows how to build a complex multiplier with latency;
- Example 4 Shift Operations shows how to implement shift operations;
- Example 5 Passing Parameters into the MCode Block shows how to pass parameters into a MCode block;
- Example 6 Optional Input Ports shows how to implement optional input ports on an MCode block;
- Example 7 Finite State Machines shows how to implement a finite state machine;
- Example 8 Parameterizable Accumulator shows how to build a parameterizable accumulator;
- Example 9 FIR Example and System Verification shows how to model FIR blocks and how to do system verification;
- Example 10 RPN Calculator shows how to model a RPN calculator – a stack machine;
- Example 11 Example of disp Function shows how to use disp function to print variable values.