Please how do I fix this: 'und1/MATLAB file (level-2) S-Function' cannot use 'set default dimension function'.?
Show older comments
I checked earlier provided answer but it seems unrelated (https://uk.mathworks.com/matlabcentral/answers/98809-why-do-i-receive-a-cannot-use-set-default-dimension-function-error-when-using-the-matrix-concatena); (>> doc signalspecification).
It shows error when output is connected to Out1 [ Simulink output block ]. It simulates well for output connected as input to a D-T SS system [ If and only if ] D-T SS output is fed back as Input.Port(7). Debugging shows that the error starts at this point [ function Output(block) ]
Please see code below: [NB: 'abc1' is a c code using struct parameters 'prt'; whose outputs are in struct form, and only '.u_o' is needed]
{ function abc_und(block) % Level-2 MATLAB file S-Function for optimal input for D-T state-space
setup(block);
%endfunction
function setup(block)
%%Register number of input and output ports
block.NumInputPorts = 8;
block.NumOutputPorts = 1;
%%Setup functional port properties to dynamically
%%inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Set Parameter dimensions
block.InputPort(1).Dimensions = [5 5]; % A (matrix)
block.InputPort(1).DirectFeedthrough = true;
block.InputPort(2).Dimensions = [5 1]; % B (vector)
block.InputPort(2).DirectFeedthrough = true;
block.InputPort(3).Dimensions = [5 5]; % Q (matrix)
block.InputPort(3).DirectFeedthrough = true;
block.InputPort(4).Dimensions = [5 5]; % P (matrix)
block.InputPort(4).DirectFeedthrough = true;
block.InputPort(5).Dimensions = 1; % R (scalar)
block.InputPort(5).DirectFeedthrough = true;
block.InputPort(6).Dimensions = 1; % S (scalar)
block.InputPort(6).DirectFeedthrough = true;
block.InputPort(7).Dimensions = [5 1]; % x0 (vector)
block.InputPort(7).DirectFeedthrough = true;
block.InputPort(8).Dimensions = 1; % u_lim (scalar)
block.InputPort(8).DirectFeedthrough = true;
%%Set block sample time to inherited
block.SampleTimes = [0.02 0];
%%Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%%Run accelerator on TLC
block.SetAccelRunOnTLC(true);
%%Register methods
block.RegBlockMethod('Outputs', @Output);
block.RegBlockMethod('WriteRTW', @WriteRTW);
%endfunction
function Output(block)
prt.A = block.InputPort(1).Data;
prt.B = block.InputPort(2).Data;
prt.Q = block.InputPort(3).Data;
prt.P = block.InputPort(4).Data;
prt.R = block.InputPort(5).Data;
prt.S = block.InputPort(6).Data;
prt.x_0 = block.InputPort(7).Data;
prt.u_max = block.InputPort(8).Data;
abc_u = abc1(prt);
block.OutputPort(1).Data = abc_u.u_0;
%endfunction
function WriteRTW(block)
abc_u = abc1(prt);
block.WriteRTWParam('string', 'Optimal_Input', abc_u.u_0);
%endfunction }
Answers (1)
Paolo
on 28 Mar 2025
0 votes
I think the issue is that you have not set the output dimensions. Doing it solved the issue for me.
Try adding:
block.OutputPort(1).Dimensions = 1;
Categories
Find more on Configure Block Features for MATLAB S-Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!