Thread Subject:
Level 2 Matlab S-Function

Subject: Level 2 Matlab S-Function

From: Kwok

Date: 7 Jun, 2012 13:27:07

Message: 1 of 3

I have a very simple simulink model which uses S-function to output a 2x2 matrix to display block. The matrix is initialized in matlab workspace, and it is input to the s-function through dialog parameter. The input to this s-function block is just the constant block with value of 1. It gives me an error message "Error in 'SetOutputPortDimensions' method of ...... The dimensions of output port 0 should have been set to [1], but was instead set to [2x2]"

I tried many different ways, but I cant get this to work. Thanks.

My code is below:
function test4(block)
setup(block);

function setup(block)
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.NumDialogPrms = 1;
block.SampleTimes = [-1 0];
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims);
block.RegBlockMethod('SetOutputPortDimensions', @SetOutPortDims);
block.RegBlockMethod('Outputs', @Outputs);

function SetInpPortFrameData(block, idx, fd)
  block.InputPort(1).SamplingMode = fd;
  block.OutputPort(1).SamplingMode = fd;

function SetInpPortDims(block, idx, fd)
  block.InputPort(1).Dimensions = 1;

function SetOutPortDims(block, idx, fd)
  block.OutputPort(1).Dimensions = [2 2];

function Outputs(block)
a=block.DialogPrm(1).Data;
block.OutputPort(1).Data = a;

Subject: Level 2 Matlab S-Function

From: Phil Goddard

Date: 7 Jun, 2012 18:49:08

Message: 2 of 3

Do you need dynamic port sizing?
If not, the following works,

%%%%%%%%%%%%%%%%cute here %%%%%%%%%%%
function test4(block)
setup(block);

function setup(block)
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).Dimensions = 1;
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).Dimensions = [2 2];
block.NumDialogPrms = 1;
block.SampleTimes = [-1 0];
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('Outputs', @Outputs);

function Outputs(block)
a=block.DialogPrm(1).Data;
block.OutputPort(1).Data = a;
%%%%%%%%%%%%%%%%cute here %%%%%%%%%%%

Subject: Level 2 Matlab S-Function

From: Tiger

Date: 18 Mar, 2013 10:23:07

Message: 3 of 3

Hi Phil,

What if the case was for dynamically setting the Dwork.Dimensions. My Dwork Vector's dimension depends on the values that comes from an input port.

I've tried the following and didn't work:
function DoPostPropSetup(block)
% Dwrok vector set-up
block.NumDworks = 1;
% Completed_T_ID.
block.Dwork(1).Name = 'Completed_T_ID';
block.Dwork(1).Dimensions = block.InputPort(3).Data;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;

Any suggestions? Thanks.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
simulink Phil Goddard 7 Jun, 2012 14:49:12
rssFeed for this Thread

Contact us