| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Embedded MATLAB |
| Contents | Index |

This part of the tutorial demonstrates how to integrate M-code into Simulink using the Embedded MATLAB Function block. The Embedded MATLAB Function block allows you to add MATLAB functions to Simulink models for simulation, testing, code generation, and/or deployment to embedded processors. It supports a subset of MATLAB commands that generate efficient C code.
To begin the process of integrating your M-code with Simulink, you work with the model emldemo_noise_cancel_01.mdl and the file emldemo_lms_05.m. Alternatively, you can view the changes described in this part of the tutorial in the model emldemo_noise_cancel_02.mdl.
The file emldemo_lms_05.m implements a least mean squares (LMS) filter. The models emldemo_noise_cancel_01.mdl and emldemo_noise_cancel_02.mdl use this filter to remove noise from an input signal. For more information, see Example: The LMS Filter.
Note If you have not already copied these files to a local folder, follow the instructions in Copying Files Locally. |
In practice, you would add an Embedded MATLAB Function Block to an empty model, and then add input and output blocks as required. To simplify this process for the purposes of this tutorial, you add the Embedded MATLAB Function Block to the emldemo_noise_cancel_01 model supplied with the tutorial.
To add an Embedded MATLAB Function block to the emldemo_noise_cancel_01 model:
Open emldemo_noise_cancel_01.mdl in Simulink:
Set your MATLAB current folder to the folder that contains your files for this tutorial. At the MATLAB command line, enter:
cd folder
where folder is the full path name of the folder containing your files. See Viewing and Changing the Current Folder Using the Current Folder Browser in the MATLAB Desktop Tools and Development Environment documentation for more information.
At the MATLAB command line, type:
emldemo_noise_cancel_01
The model opens:

Add an Embedded MATLAB Function block to the model:
At the MATLAB command line, type simulink to open the Simulink library browser.
From the list of Simulink libraries, select the User-Defined Functions library.
Click the Embedded MATLAB Function block and drag it into the emldemo_noise_cancel_01 model. Place the block just above the red text annotation Place Embedded MATLAB Function Block here.
Your model should now look like this:

Delete the red text annotations from the model.
Save the model in the current folder as my_emldemo_noise_cancel_01.mdl.
In the Simulink Model Editor, select Save As from the File menu.
Enter my_emldemo_noise_cancel_01.mdl as the new model name.
Click Save.
You now use the Embedded MATLAB Function block to call your existing function emldemo_lms_05. To add your M-code to the Embedded MATLAB Function block:
Double-click the Embedded MATLAB Function block to open the Embedded MATLAB editor.
Delete the default code displayed in the Embedded MATLAB editor.
Copy the following code to the Embedded MATLAB Function block.
function [ Signal_Out, Weights ] = ...
LMS(Adapt, Reset, Noise_In, Signal_In) %#eml
% Compute LMS:
[ ~, Signal_Out, Weights ] = ...
emldemo_lms_05(Noise_In, Signal_In, Reset, Adapt);
end
Because the LMS function does not use the first output from emldemo_lms_05, replace this output with the MATLAB ~ operator. Embedded MATLAB ignores inputs and outputs specified by ~. This syntax helps avoid confusion in your program code and unnecessary clutter in your workspace.
Resize the Embedded MATLAB Function block. The emldemo_lms_05 function inputs Noise_In, Signal_In, Reset and Adapt now appear as input ports to the block and the function outputs Signal_Out and Weights appear as output ports.

Save the model by selecting File > Save from the Embedded MATLAB editor menu.
The next step is to connect the Embedded MATLAB function block inputs and outputs.
Make the following connections:
| From... | To... |
|---|---|
| Settings > Adapt | Embedded MATLAB Function Block > Adapt |
| Settings > Reset | Embedded MATLAB Function Block > Reset |
| White Noise | Embedded MATLAB Function Block > Noise_In |
| Acoustic_Environment > Signal_Out | Embedded MATLAB Function Block > Signal_In |
| Embedded MATLAB Function Block > Signal_Out | To Audio Device |
| Embedded MATLAB Function Block > Signal_Out | Analysis and Visualization > Signal |
| Embedded MATLAB Function Block > Weights | Analysis and Visualization > Weights |
See Connecting Blocks in the Simulink documentation for more information.
Your model should now look like this:

Because the Analysis and Visualization block expects to receive a frame-based signal from the LMS filter, you must configure the Embedded MATLAB Function block Signal_Out output parameter to be frame-based rather than sample-based.
Double-click the Embedded MATLAB Function block to open the Embedded MATLAB editor.
Select Tools > Edit Data/Ports to open the Ports and Data Manager.
Select the signal called Signal_Out from the list in the left pane.

On the General tab, change the Sampling mode from Sample based to Frame based.
Click the Apply button.
Close the Ports and Data Manager and the Embedded MATLAB editor.
Save the model.
You are now ready to run the model.
To run the model:
Ensure that you can see the Time Domain plots.
To view the plots, in the emldemo_noise_cancel_01 model, open the Analysis and Visualization block and then open the Time Domain block.
Select Simulation > Start in the Simulink model window.
While the model is running, you can double-click the Reset and Adapt settings to control the operation of the filter. When Adapt is enabled, the filter continuously updates the filter weights. When Adapt is disabled, the filter weights remain at their current values. If Reset is set, the filter resets the filter weights.
For standard operation, you should enable the Adapt signal and disable the Reset signal.
As Simulink runs the model, you see and hear outputs. Initially, you hear the audio signal distorted by noise. Then, during the first few seconds, the filter attenuates the noise gradually, until you hear only the music playing with very little noise remaining. MATLAB displays the following plot showing filter convergence after only a few seconds.

| To... | See... |
|---|---|
Learn more about using the Embedded MATLAB Function block | Using the Embedded MATLAB Function Block in the Simulink User's Guide |
Learn more about the best practices used in this tutorial |
![]() | Tasks for Integrating with Simulink | Best Practices for Working with the Embedded MATLAB Subset | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |