Main Content

This section explains how to send data from a Simulink^{®} model to the MATLAB^{®} workspace so you can analyze the results of simulations in greater
detail.

You can use a To Workspace (Simulink) block, from the DSP System Toolbox™/Sinks library to send data to the MATLAB workspace as a vector. For example, you can send the error rate data from the Hamming code model, described in the section Reducing the Error Rate Using a Hamming Code. To insert a To Workspace (Simulink) block into the model, follow these steps:

To open the model, at the MATLAB prompt, enter

`doc_hamming`

.To add a To Workspace (Simulink) block, begin typing the name 'to workspace' in the model window and select the To Workspace block from the DSP System Toolbox/Sinks library. Connect it as shown.

**Tip**

More than one To Workspace block exists. Select the To Workspace block from the DSP System Toolbox / Sinks sublibrary.

To configure the To Workspace (Simulink) block, follow these steps:

Double-click the block to display its dialog box.

Type

`hammcode_BER`

in the**Variable name**field.Type

`1`

in the**Limit data points to last**field. This limits the output vector to the values at the final time step of the simulation.Click

**OK**.

When you run a simulation, the model sends the output of the Error Rate Calculation
block to the workspace as a vector of size 3, called `hamming_BER`

. The
entries of this vector are the same as those shown by the Error Rate Display block.

After running a simulation, you can view the output of the To Workspace (Simulink) block by typing the following commands at the MATLAB prompt:

format short e hammcode_BER

The vector output is the following:

```
hammcode_BER =
```

5.4066e-003 1.0000e+002 1.8496e+004

The command `format short e`

displays the entries of the vector in
exponential form. The entries are as follows:

The first entry is the error rate.

The second entry is the total number of errors.

The third entry is the total number of comparisons made.

To analyze the error-correction performance of the Hamming code, send the transmitted signal, the received signal, and the error vectors, created by the Binary Symmetric Channel block, to the workspace. An example of this is shown in the following figure.

Type

`doc_channel`

at the MATLAB command line to open the starter model.Double-click the Binary Symmetric Channel block to open its dialog box, and select

**Output error vector**. This creates an output port for the error data.Move blocks to make room so that you can insert Hamming Encoder and Hamming Decoder blocks. To find them, start typing

`Hamming`

in the model window. Select them from the options presented. These Hamming Encoder and Hamming Decoder blocks are in the Communications Toolbox™/Error Detection and Correction /Block sublibrary.Add three To Workspace (Simulink) blocks into the model window and connect them as shown in the preceding figure.

**Tip**More than one To Workspace block exists. Select the To Workspace block from the DSP System Toolbox / Sinks sublibrary.

Double-click the left To Workspace (Simulink) block.

Type

`Tx`

in the**Variable name**field in the block's dialog box. The block sends the transmitted signal to the workspace as an array called`Tx`

.In the

**Save 2-D signals as**field, select`3-D array (concatenate along third dimension)`

. This preserves each frame as a separate column of the array`Tx`

.Click

**OK**.

Double-click the middle To Workspace (Simulink) block:

Type

`errors`

in the**Variable name**field.In the

**Save 2-D signals as**field, select`3-D array (concatenate along third dimension)`

. This preserves each frame as a separate column of the array`Tx`

.Click

**OK**.

Double-click the right To Workspace (Simulink) block:

Type

`Rx`

in the**Variable name**field.In the

**Save 2-D signals as**field, select`3-D array (concatenate along third dimension)`

. This preserves each frame as a separate column of the array`Tx`

.Click

**OK**.

After running a simulation, you can display individual frames of data. For example, to
display the tenth frame of `Tx`

, at the MATLAB prompt type

Tx(:,:,10)

This returns a column vector of length 4, corresponding to the length of a message word.
Usually, you should not type `Tx`

by itself, because this displays the
entire transmitted signal, which is very large.

To display the corresponding frame of errors, type

errors(:,:,10)

This returns a column vector of length 7, corresponding to the length of a codeword.

To display frames 1 through 5 of the transmitted signal, type

Tx(:,:,1:5)

You can use MATLAB to analyze the data from a simulation. For example, to identify the differences between the transmitted and received signals, type

`diffs = Tx`

~`=Rx`

;

The vector `diffs`

is the XOR of the vectors `Tx`

and
`Rx`

. A 1 in `diffs`

indicates that
`Tx`

and `Rx`

differ at that position.

You can determine the indices of frames corresponding to message words that are incorrectly decoded with the following MATLAB command:

error_indices = find(diffs);

A 1 in the vector `not_equal`

indicates that there is at least one
difference between the corresponding frame of `Tx`

and
`Rx`

. The vector `error_indices`

records the indices
where `Tx`

and `Rx`

differ. To view the first incorrectly
decoded word, type

Tx(:,:,error_indices(1))

To view the corresponding frame of errors, type

errors(:,:,error_indices(1))

Analyze this data to determine the error patterns that lead to incorrect decoding.