Error due to variable size data in Matlab function block

Hi all,
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
Doesn't compile because
"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.
Thanks for any help, Luca

 채택된 답변

Kaustubha Govind
Kaustubha Govind 2013년 6월 17일

1 개 추천

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.

댓글 수: 3

Thanks for your reply. What you say is true if you consider the size of a signal as variable at runtime. In my case it is decided at compile time by dataType, which is not an input but a parameter, defined in the mask I put above the embedded code block. I apologize because I didn't specify this.
In this case it is indeed possible to do what I wanted, and compile that code. The problem was simply that dataType had been marked as "tunable" after creation, and I didn't noticed it. This made ak variable sized at runtime, and if it was intended I agree that the only solution to overcome the error would be the one you outlined. Best wishes
Great! Thanks for posting your analysis!
I had similar issue. Because 'tunable' was marked for a parameter defining size of output matrix, MATLAB was guessing the size as variable...thank you for the answer...!!!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Communications Toolbox에 대해 자세히 알아보기

질문:

2013년 6월 16일

답변:

2020년 11월 25일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by