dsp.AsyncBuffer
Object Give a Dimension Mismatch Error in the MATLAB Function
Block?If you are reading data from an asynchronous buffer inside a MATLAB Function (Simulink) block, the block throws
a dimension mismatch error if the output of the read
method is not specified to be a variable-size signal.
Here is the bufferWrapper
function that contains the algorithm
inside the MATLAB Function block. When input on the
cmd
port is 1
, the dsp.AsyncBuffer
object writes the data input, u
, to the
buffer. When input on the cmd
port is 0
, the
object reads data from the buffer.
function [y,isData] = bufferWrapper(u,cmd) persistent asyncBuff if isempty(asyncBuff) asyncBuff = dsp.AsyncBuffer; setup(asyncBuff,u); end if cmd % write write(asyncBuff,u); y = zeros(3,1); isData = false; else % read y = read(asyncBuff,3); isData = true; end
You must initialize the buffer by calling either write
or setup
before the first call to
read
.
During the write operation, the first output, y
, is
zeros(3,1)
and the second output, isData
, is
0. During the read operation, y
is the data in the buffer and
isData
is 1.
Run the model and the following error occurs.
The output of read(asyncBuff,3)
on line 14 is variable sized. The
output is variable sized because the size of the signal output by the read
function depends on the input arguments to read
. To resolve this error, specify y
as a variable-size signal.
In the MATLAB Function block Editor, click Edit Data to open the Ports and Data Manager.
For the output y
, select the Variable
size check box.
Click Apply.
Run the model and view the output y
in the Time
Scope.
With cmd = 0
, no data is written into the buffer. Therefore, the
output is 0. To write the input data u
to the buffer, set
cmd = 1
. After you write some data, if you change
cmd
back to 0, the Time Scope output changes to
the following.