Level-2 MATLAB S-Functions with unknown fixed input / output sizes
1 view (last 30 days)
Show older comments
Hi all.
I am looking for help regarding the definition of a Level-2 S-Function with unknown input size. My use case foresees 10 inputs, 6 of which are fixed size ones - and do not give us any problem so far - and 4 of unknown dimensions, which remain fixed during the simulation. We are faced with a run time error (Error in port widths or dimensions) even if we set (or we assume to set) all input ports to have a inherited size.
We read countless Q&A here on MatlabCentral, but none completely helped us out. Even the one from the Mathworks support (this one) does not work when modified. Below I'll use it to showcase the issue.
What follows is the S-Function in its entirety. We prepare to have in/outport to be set to be dynamically sized with
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
and only explicitly set the size of outport(1) to 3 and outport(2) to 1.
function matlabSfunUnknownSize(block)
setup(block);
end
function setup(block)
% Register number of ports
block.NumInputPorts = 1;
block.NumOutputPorts = 2;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
% block.SetPreCompOutPortInfoToDynamic;
% Override input port properties
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = false;
% Override output port properties
block.OutputPort(1).Dimensions = 3; % <------- remove this line to have the error change
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(2).Dimensions = 1;
block.OutputPort(2).DatatypeID = 0; % double
block.OutputPort(2).Complexity = 'Real';
% Register parameters
block.NumDialogPrms = 0;
% Register sample times
block.SampleTimes = [-1 0];
% Specify the block simStateCompliance. The allowed values are:
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Terminate', @Terminate); % Required
block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
end
function Outputs(block)
block.OutputPort(1).Data = [1 2 3];
block.OutputPort(2).Data = sum(block.InputPort(1).Data, 'all');
end
function Terminate(block)
% no-op
end
function SetInpPortFrameData(block, idx, fd)
block.InputPort(idx).SamplingMode = fd;
block.OutputPort(1).SamplingMode = fd;
block.OutputPort(2).SamplingMode = fd;
end
What we expect is the s-function block to automatically accept 'any' input size. In the Simulink model attached we set its input to a constant value, namely a ones(5,2) matrix. The size is not correctly propagated to the s-function block:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/741474/image.jpeg)
and the following error is thrown: Error in port widths or dimensions. 'Output Port 1' of 'model/Constant' is a [5x2] matrix.
If instead one output port has its dimensions not explicitly declared in the S-Function body, i.e. if line
block.OutputPort(1).Dimensions = 3;
is removed from the previous listings, then the model correctly accepts [5, 2] as input and sets input(1) dimensions to [5, 2], but also imposes [5, 2] as output(1) size, thus generating another runtime error.
How can we set one (or more) inputs to have unknown, fixed size, whilst (obviously) not having error thrown and maintaining all outports of fixed, known size?
Attached you can find the S-Function body and the test Simulink model.
Thanks
0 Comments
Answers (0)
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!