sFunction for matrix calculation of matrices with different ranges

Hey all!
I am looking for a Level 2 MATLAB sFunction for Simulink which can handle matrices with different ranges.
What I tried is:
function anregung(block)
% Level-2 MATLAB file S-Function for times two demo.
% Copyright 1990-2009 The MathWorks, Inc.
% $Revision: 1.1.6.2 $
setup(block);
%endfunction
function setup(block)
%%Register number of input and output ports
block.NumInputPorts = 3;
block.NumOutputPorts = 1;
%%Setup functional port properties to dynamically
%%inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DirectFeedthrough = true;
block.InputPort(2).DirectFeedthrough = true;
block.InputPort(3).DirectFeedthrough = true;
%%Set up the inport data-type properties.
block.InputPort(1).DatatypeID = -1;
block.InputPort(1).Complexity = 'Real';
%%Set up the outport data-type properties.
block.OutputPort(1).DatatypeID = -1;
block.OutputPort(1).Complexity = 'Real';
%%Set block sample time to inherited
block.SampleTimes = [0 0];
%%Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%%Run accelerator on TLC - Eine TLC Datei wird im Falle von 'true' erzeugt
block.SetAccelRunOnTLC(true);
%%Set the number of Parameters
block.NumDialogPrms = 3;
%%Register methods
block.RegBlockMethod('Outputs', @Output);
block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims);
block.RegBlockMethod('SetOutputPortDimensions', @SetOutPortDims);
%endfunction
function Output(block)
M_kappa = block.DialogPrm(1).Data;
R_kappa = block.DialogPrm(2).Data;
h_delta = block.DialogPrm(3).Data;
block.OutputPort(1).Data = (M_kappa\R_kappa)*h_delta;
%endfunction
M_kappa is a 3x3 Matrix, R_kappa 3x2 and h_delta is a 2x1 vector. As you can see, I'd just like to devide the matrices and multiplicate them with the vector.
The point is, I get three errors:
  1. Item one 'Error in port widths or dimensions. Output port 1 of 'modele/Constant1' is a [3x2] matrix.'
  2. Item two 'Error evaluating registered method 'SetInputPortDimensions' of MATLAB S-Function 'anregung' in 'model/Level-2 MATLAB S-Function'. The dimensions of output port 1 of 'model/Level-2 MATLAB S-Function' cannot be changed from [3x3] to [3x2]. The following is the MATLAB call stack (file names and line numbers) that produced this error: ['C:...path\Simulink\anregung.m'] [92]'
  3. Item three 'The dimensions of output port 1 of 'model/Level-2 MATLAB S-Function' cannot be changed from [3x3] to [3x2].'
Anybody an idea how to define the input dimensions properly?
Many thanks in advance!
Greeeetz Drej

7 Comments

How do you define SetInpPortDims and SetOutPortDims?
The inputportdims code I use is
function SetInpPortDims(s, idx, di)
% Set dimension mode
block.InputPort(idx).Dimensions = di;
block.OutputPort(1).Dimensions = di;
function SetOutPortDims(s, idx, di)
% Set dimension mode
block.InputPort(1).Dimensions = di;
block.OutputPort(idx).Dimensions = di;
Edit: I include the matrices by 'Constant'-blocks which are connected to the ports - for the case that this information does play a role.
I am confused by your implementation of SetInpPortDims and SetOutPortDims. Which input's dimensions does the output port dimension match with exactly? You seem to overwriting block.OutputPort(1).Dimensions for all values of idx. Also, why force block.InputPort(1).Dimensions to be the same as the output port dimensions?
well, basicly, it is my first sfuncion.:) So this is why I just tried to take few components from the matlab library and include them to my own code.
The point is, my sfunction does not recognize the dimensions of the inputs. Now I deleted the OutputPort-lines and still get the error
'Error in 'SetOutputPortDimensions' method of 'model/Level-2 MATLAB S-Function'. The dimension of input port 0 should have been set to [3x3], but was instead set to [1x1]'.
I just don't get the way how to define input port dimensions depending on the dimensions of the signal.
In case I delete the function SetInpPortDims(s,idx,di) and set the
block.InputPort('every port').DirectFeedthrough
to true, I get the error, that the outport of the 'Constant'-R_kappa, which is a [3x2] matrix does not correspond to the input port 2 of the sfunction, because it is a [3x3] matrix.
to clarify ma problem I added an image of my simple model and in addition the entire code.
function anregung(block)
% Level-2 MATLAB file S-Function for times two demo.
% Copyright 1990-2009 The MathWorks, Inc.
% $Revision: 1.1.6.2 $
setup(block);
%endfunction
function setup(block)
%%Register number of input and output ports
block.NumInputPorts = 3;
block.NumOutputPorts = 1;
%%Setup functional port properties to dynamically
%%inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DirectFeedthrough = true;
block.InputPort(2).DirectFeedthrough = true;
block.InputPort(3).DirectFeedthrough = true;
%%Set block sample time to inherited
block.SampleTimes = [0 0];
%%Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%%Run accelerator on TLC - Eine TLC Datei wird im Falle von 'true' erzeugt
block.SetAccelRunOnTLC(true);
%%Set the number of Parameters
block.NumDialogPrms = 3;
%%Register methods
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('Outputs', @Output);
function SetInpPortDims(s, port, di)
% Set dimension mode
block.InputPort(port).Dimensions = di;
%endfunction
function Output(block)
M_kappa = block.DialogPrm(1).Data;
R_kappa = block.DialogPrm(2).Data;
h_delta = block.DialogPrm(3).Data;
block.OutputPort(1).Data = (M_kappa\R_kappa)*h_delta;
%endfunction
No other suggestions?

Sign in to comment.

Answers (0)

Products

Asked:

on 26 Sep 2013

Commented:

on 27 Sep 2013

Community Treasure Hunt

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

Start Hunting!