Error due to variable size data in Matlab function block

W
Wyatt · Dec 7, 2020 · 2.9K views
Question
I have a problem that many others had before, so I read some discussions and references before asking the following: I have a Matlab function block in Simulink, which would like to be a modulator. It gets as input a [nx1] frame of data and should return a an [lx1] frame, where either l=n or l = n/K, for some K which divides n. However, the (very simplyfied extract of) code   function ak = Modulator(dataFrame,dataType) coder.varsize('ak',length(dataFrame)); M = 4; if dataType == 1 %input is a binary stream, bit mapping required ak = zeros(round(length(dataFrame)/log2(M)),1); else % input data is a stream of integer ak = zeros(length(dataFrame),1); end end "Data 'ak' is inferred as a variable size matrix, while its specified type is something else." Now, in line 2 I specified that it is a variable size matrix, and I also used an if/else constructor to initialize it. To make the compiler happy, one may check the "Variable number of columns" checkbox for ak, in the Data and Ports Manager, but this turns out in a new error, because the blocks in cascade don't accept variable data, at least the ones I need, like digital filters.
Expert Answer
Profile picture of John Michell
John Michell PhD Expert
Answered Aug 26, 2026

Since your signal changes size at run-time, configuring the MATLAB Function block to output a variable-size signal using the Ports and Data Manager is the right thing to do. If the rest of the blocks do not accept variable-size signals, then you will need to deal with that issue, perhaps by limiting yourself to blocks that do accept variable-size signals, or implementing custom blocks to do what you need.

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!