Setting output of a continuous S-Function

조회 수: 7 (최근 30일)
Chris Lim
Chris Lim . 2016년 6월 15일
답변: Dhruvesh Patel . 2016년 8월 8일
I have a continuous S-Function that solves the derivatives for various state properties within a ICE cylinder. As such, the output of the function is set to output the integral of those derivatives for each timestep which is a 7 element vector (1 for each of the properties being calculated)
block.OutputPort(1).Data = block.ContStates.Data;
At some point in the cycle I would like to change the value of this output to an explicit value (ie. not the integral of the derivative) such as below (in this case the explicit value being x):
block.OutputPort(1).Data = block.ContStates.Data;
block.OutputPort(1).Data(3) = x;
I'm using a if statement to switch into this alternative output. Through debugging, I think I've verified that Simulink is entering this block of code at the right point - however the output of the S-Function remains as the integral of the derivatives which gives me a non-finite error.
Any help or suggestions that would allow me to explicitly set the output of a continous S-Function would be much appreciated.

답변 (1개)

Dhruvesh Patel
Dhruvesh Patel 2016년 8월 8일
If I am understanding correctly, you are trying to update the output of a MATLAB S-function block conditionally.
The following code depicts a simplified representative case which you can adapt to suit your requirement.
function myMSfunc2(block)
% Level-2 MATLAB file S-Function for continuous time demo
function setup(block)
%%Register number of input and output ports
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
%%Setup functional port properties to dynamically
block.InputPort(1).Dimensions = 2;
block.InputPort(1).DirectFeedthrough = true;
block.OutputPort(1).Dimensions = 2;
%%Set block sample time to continuous
block.SampleTimes = [0 0];
%%Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%%Register methods
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('InitializeConditions', @InitConditions);
block.RegBlockMethod('Outputs', @Output);
block.RegBlockMethod('Update', @Update);
function DoPostPropSetup(block)
%%Setup Dwork for data logging/states
block.NumDworks = 1;
block.Dwork(1).Name = 'X';
block.Dwork(1).Dimensions = 2;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = true;
function InitConditions(block)
%%Initialize Dwork
block.Dwork(1).Data = [0;0];
function Output(block)
block.OutputPort(1).Data = block.Dwork(1).Data;
% changing the output conditionally
block.OutputPort(1).Data(1) = 2;
function Update(block)
block.Dwork(1).Data = block.InputPort(1).Data;
The Output function has an if statement which updates the output based on a condition; which is what you are looking for. Also, notice the DWork vector used to store state information. It is recommended to use Work vectors instead of global variables to store persistent data. More about DWork vectors can be found here. Also check the article on Writing MATLAB S-functions for more information.

Community Treasure Hunt

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

Start Hunting!

Translated by