出现需要使用'Level-2 MATLAB S-Function'的错误

27 views (last 30 days)
伟鹏
伟鹏 on 10 Jul 2024
Answered: Shantanu Dixit on 13 Sep 2024
S函数程序为
function calculate_gain_sfunc(block)
setup(block);
end
function setup(block)
% Register the number of ports
block.NumInputPorts = 3; % Q1, Q2, and F2(t)
block.NumOutputPorts = 2; % y1 and y2
% Set up the input and output ports
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).Dimensions = 1; % Q1
block.InputPort(2).Dimensions = 1; % Q2
block.InputPort(3).Dimensions = 1; % F2
block.OutputPort(1).Dimensions = 1; % y1
block.OutputPort(2).Dimensions = 1; % y2
% Register the parameters
block.NumDialogPrms = 9; % J1, L1, R1, AE, F1, J2, L2, R2
% Set the sample time
block.SampleTimes = [0 0];
% Specify the block simStateCompliance
block.SimStateCompliance = 'DefaultSimState';
% Register the methods
block.RegBlockMethod('Outputs', @Output); % Register the output method
end
function Output(block)
% Get the inputs
Q1 = block.InputPort(1).Data;
Q2 = block.InputPort(2).Data;
F2 = block.InputPort(3).Data;
% Get the parameters
J1 = block.DialogPrm(1).Data;
L1 = block.DialogPrm(2).Data;
R1 = block.DialogPrm(3).Data;
AE = block.DialogPrm(4).Data;
F1 = block.DialogPrm(5).Data;
J2 = block.DialogPrm(6).Data;
L2 = block.DialogPrm(7).Data;
R2 = block.DialogPrm(8).Data;
% Compute the gains
gain1 = J1 * L1 / (R1 * (AE - F1));
gain2 = J2 * L2 / (R2 * (F2 - AE));
% Compute the outputs
y1 = gain1 * Q1;
y2 = gain2 * Q2;
% Set the outputs
block.OutputPort(1).Data = y1;
block.OutputPort(2).Data = y2;
end
模型如上图所示
simulink运行结果为:
The specified MATLAB File 'calculate_gain_sfunc' in 'sys/S-Function1' is not a valid Level 1 S-Function. The number of input and/or output arguments is not valid. A likely cause for this error is that the name of a level-2 MATLAB S-function has been specified in this block (which supports only level-1 MATLAB S-functions). Use the 'Level-2 MATLAB S-Function' block from the Simulink library

Answers (1)

Shantanu Dixit
Shantanu Dixit on 13 Sep 2024
Hi 伟鹏 ,
The issue arises from the fact that the above S-function code is a 'Level-2 MATLAB S-function', but the block used supports 'Level-1 S-functions' only. The setup(block) method, along with features such as dynamic port handling and the use of block.RegBlockMethod to register callbacks, are specific to Level-2 S-functions.
To resolve this, you can switch from the standard 'S-Function' block to the 'Level-2 MATLAB S-Function' block in Simulink. This will ensure that the methods and functionality of your Level-2 S-function code are compatible with the block you're using.
Additionally refer to the following MathWorks documentation to know more about Level-2 MATLAB methods:

Tags

Community Treasure Hunt

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

Start Hunting!