Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Integrate MATLAB Algorithm in Model

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;

Defining Local Variables for Code Generation

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 function stats, the local variable len is 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).

Related Examples

More About

Was this topic helpful?