Here is an example of a Simulink® model that contains a MATLAB Function block:
The MATLAB Function block contains the following algorithm:
function [mean,stdev] = stats(vals) % calculates a statistical mean and a standard % deviation for the values in vals. len = length(vals); mean = avg(vals,len); stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len); plot(vals,'-+'); function mean = avg(array,size) mean = sum(array)/size;
You build this model in Create Model That Uses MATLAB Function Block.
If you intend to generate code
from the MATLAB® algorithm in a MATLAB Function block,
you must explicitly assign the class, size, and complexity of local
variables before using them in operations or returning them as outputs
(see Data Definition for Code Generation. In the example
stats, the local variable
defined before being used to calculate mean and standard deviation:
len = length(vals);
Generally, once you assign properties to a variable, you cannot redefine its class, size, or complexity elsewhere in the function body, but there are exceptions (see Reassignment of Variable Properties).