Variable-Sized Output from MATLAB Function block

조회 수: 16 (최근 30일)
Greg
Greg 2014년 4월 29일
답변: Ramon Fanini 2020년 10월 13일
I'm trying to create a MATLAB Function block with a variable-sized output in a Simulink model (R2014A and R2013B).
All that's in my Function block at the moment is the following:
function param_value = param_access(param_number)
%#codegen
coder.varsize('param_number',[1 2]);
coder.varsize('param_value',[1 2]);
param_value = zeros(size(param_number));
The input "param_number" is a variable-sized signal coming in from Simulink. Both the input and the output are marked as "Variable Sized" in the MATLAB Function Block Port manager. The input seems to be handled OK. However, the output signal is persistently retruning an error like "Output 'param_value' (#1128) has variable size but the upper bound is not specified; explicit upper bound must be provided."
As you can see, I believe that I have specified an upper bound through the coder.varsize function, but Simulink doesn't seem to be accepting it for some reason.
I'm also getting another error like "Error in port widths or dimensions. Output port 1 of 'untitled/MATLAB Function/param_number' is a one dimensional vector with a maximum of 2 elements." This one's a little funny because "param_number" is the input, not the output, so I think this is related to the hidden behind-the-scenes way the MATLAB Function block is implemented. This error always attaches itself to the last input of the MATLAB Function block, whatever that is, even if that signal itself is not actually variable-sized and just has a fixed size of [1 1].

채택된 답변

Ryan Livingston
Ryan Livingston 2014년 4월 29일
You should also set up the sizes in the Data Editor for the MATLAB Function Block as per:
So, in the editor for your MATLAB Function Block code click "Edit Data" and check the variable-size box for param_value and give [1 2] as the upper bound/size. Doing this alerts Simulink to the fact that the data should be variable-size.
  댓글 수: 3
Noam Greenboim
Noam Greenboim 2015년 2월 4일
In addition to that, if this output goes later to a "To Workspace" block, make sure that you set the "save format" field as structure or structure with time.
timo
timo 2016년 9월 18일
편집: timo 2016년 9월 18일
coder.varsize(...); is mandatory in the beginnng of the function if you dont add comment %codegen on the same line as function name
I wonder if this is a bug

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

추가 답변 (2개)

timo
timo 2016년 9월 18일
For the function bellow , y is variable size
function y = addEscapeCharacter(frameBytes, escapeCharacter, startOfFrame)
%codegen
finalArray = single([]);
coder.varsize('y',[1,10]);
if randi(2) ==1
finalArray = horzcat(single( [1 2 3 4 5 6 7 8 9]) , single([6]));
else
finalArray = horzcat(single( [1 2 3 4 5 6 7]), single([6]), single([1]));
end
y = single(finalArray);
This is mandatory coder.varsize('y',[1,10]); also click on Edit data button when editing the MATLAB function , select variable size and add uper and lower bounds as [val1, val2], so that means the y output can be an array of size val1 to val2

Ramon Fanini
Ramon Fanini 2020년 10월 13일
v = polyfit (Time, Distance, 1) % extract the 1st element of the polyfit, p coefficients by coding p(1) and assign this to a new variable, V
disp('Speed of Sound, (m/s) ='), disp V % display the value of V

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by