Port Dimensions Error in S-Function

1 view (last 30 days)
Hugo Pontes
Hugo Pontes on 10 Feb 2021
Commented: Suman on 29 Jul 2024
Hello,
I'm trying to make an S-function that will perform the element wise multiplication of two vectors, in fact, I want to replicate the "Product" block with an S-Function.
I'm trying to start simple so my code doesn't feature matrix multiplication yet, since I think that that part will be quite trivial once I get the port dimensions working.
I'm getting the following error when I try to use a size 4 vector and a size 1 vector:
Error in port widths or dimensions. Output port 1 of 'untitled/Constant1' is a one dimensional vector with 1 elements.
Component:Simulink | Category:Model error
Error in port widths or dimensions. Input port 2 of 'untitled/S-Function7' is a one dimensional vector with 4 elements.
Component:Simulink | Category:Model error
I want the input and output ports to be dynamically sized so that I can input a vector of any size that will be multiplied by a vector of the same size or size 1.
As a result my code is as follows:
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams( S, 0); /*number of input arguments*/
if (!ssSetNumInputPorts(S, 2)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortWidth(S, 1, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 1, 1);
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetNumSampleTimes( S, 1);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *y = ssGetOutputPortRealSignal(S,0);
*y = 5; /* %This is of course just a placeholder for the algorithm I'll be using'
}
Only the mdlInitializeSizes(SimStruct *S) executes before I get an error but I don't understand why. In the model, when I run it, the second input port of the S-function, is expecting a size 4 vector as input when I'm giving it a size 1 vector. But why is it expecting so? Thanks in advance for any help

Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!