Hi, everybody, I got one problem when using S-function dynamic output. The input dimensions of my S function module is constant, i.e., 4,
Show older comments
Hi, everybody, I got one problem when using S-function dynamic output. The input dimensions of my S function module is constant, i.e., 4, But the dimensions of my output is dynamic, it is relative to but not the same as the values of one input. Thus, my question is how I set the number of dimension for output in a dynamic or variable way. And I check the matlab document, What it says is the dimension of output is needed to be the same as the dimension of input if we get to use variable output dimensions.
Answers (2)
Kaustubha Govind
on 10 Aug 2012
1 vote
You haven't mentioned if you're writing a MATLAB S-function, or a C S-function, but this page in the documentation points to demos for both types of S-functions with variable-sized outputs.
13 Comments
Yiqi Liu
on 13 Aug 2012
Kaustubha Govind
on 13 Aug 2012
Variable-size signals in Simulink require you to specify the maximum (upper limit) size that the signal can attain. So you need to set the maximum size in SetInputPortDims(), and set the actual size based on the input value in the Outputs() function by assigning block.OutputPort(1).CurrentDimensions.
Yiqi Liu
on 16 Aug 2012
Edited: Walter Roberson
on 16 Aug 2012
Yiqi Liu
on 16 Aug 2012
Edited: Walter Roberson
on 16 Aug 2012
Kaustubha Govind
on 16 Aug 2012
You've set your sample time to be inherited:
block.SampleTimes = [-1 0];
Perhaps the blocks connected to the input/output of the S-function are configured for continuous-time? Turning on Sample Time Colors might help.
Yiqi Liu
on 17 Aug 2012
Edited: Walter Roberson
on 17 Aug 2012
Kaustubha Govind
on 17 Aug 2012
Please see the demo I pointed in my initial answer which shows you how to setup maximum dimensions, etc. As I commented before:
"Variable-size signals in Simulink require you to specify the maximum (upper limit) size that the signal can attain. So you need to set the maximum size in SetInputPortDims(), and set the actual size based on the input value in the Outputs() function by assigning block.OutputPort(1).CurrentDimensions."
Please look at SetInputPortDims() and Outputs() function in the demo that I pointed to.
Yiqi Liu
on 20 Aug 2012
Kaustubha Govind
on 21 Aug 2012
Yiqi: What version of MATLAB are you using? Are you saying that when you enter "msfcndemo_varsize" at your MATLAB prompt, the demo doesn't open up? Note that variable-size signals were introduced in Simulink in R2009b, so if you don't have access to that model, it is likely that you are using an older version, and cannot avail of this feature.
Yiqi Liu
on 23 Aug 2012
Isaac De La Cruz
on 13 Jun 2019
"Variable-size signals in Simulink require you to specify the maximum (upper limit) size that the signal can attain. So you need to set the maximum size in SetInputPortDims(), and set the actual size based on the input value in the Outputs() function by assigning block.OutputPort(1).CurrentDimensions."
Is this also valid for C S-functions? I am trying to develop a C S-function with the same funcionality, but I can't set the size of the output to be bigger than the initial size set using ssSetOutputPortWidth. Here is the error I get:

Richard Crozier
on 22 Aug 2023
link to documentation is dead
Walter Roberson
on 22 Aug 2023
Yiqi Liu
on 12 Aug 2012
0 votes
1 Comment
Sathish Kumar
on 10 Jun 2014
Yes you can get multi-dimensional output out of a 1D input. Just remember to set the correct output port dimension.( block.OutputPort(portid).Dimensions=dim, where dim=[m,n] for an mxn output.), It your outsize changes based on the logic in your program set the output port dimension to -1.
Categories
Find more on Configure Block Features for MATLAB S-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!