Level II S-function input=output ports are not apearing

8 次查看(过去 30 天)
Hi everyone,
I am using an level II S-Function to send the position of a robot from Simulink to the MATLAB workspace. In the workspace, a controller computes the appropriate control commands and sends them back to Simulink via another Level-2 S-Function.
In the second S-Function, which is responsible for sending the computed control commands from the workspace to Simulink, I have defined two output ports and zero input ports. However, the Simulink block shows a single input port (due to the argument block) and no output ports.
Can someone explain what I should do to make the output ports visible?
I enclosed the picture of the block in Simulink.
(I am using 2019b release)
Thanks in advance
Here is my code for the second Level-2 S-Function:
function getThrust(block)
setup(block);
end
function setup(block)
block.NumInputPorts = 0
block.NumOutputPorts = 2
block.OutputPort(1).Dimensions = 4;
block.OutputPort(2).Dimensions = 4;
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(2).Complexity = 'Real';
block.OutputPort(1).DatatypeID = 0;
block.OutputPort(2).DatatypeID = 0;
block.SampleTimes = [0.1 0]; % stepping time of 0.1 [sec.]
block.RegBlockMethod('Outputs',@Outputs);
end
function Outputs(block)
try
RPM_in = evalin('base', 'thrusterPower');
catch
RPM_in = zeros(1,4);
end
try
alpha_in = evalin('base', 'thrusterAngles')
catch
angles = zeros(1,4);
end
RPM_in = reshape(RPM_in, [1,4]);
alpha_in = reshape(alpha_in, [1,4]);
block.OutputPort(1).Data = RPM_in;
block.OutputPort(2).Data = alpha_in;
end
  2 个评论
Aabha
Aabha 2025-2-11
I tried using the code you have provided for the Level-2 MATLAB S-Function, but I don’t seem to face this issue. Here are the steps I followed:
  1. Created a MATLAB Script titledgetThrust.m” and copied the code you have provided into it.
  2. Created a blank Simulink Model in the same directory as the getThrust.m” file and added a “Level-2 MATLAB S-Function” block. In the block parameters, I specified the file name “getThrust” in the “S-Function name” dialog.
After updating the model, I can see the S-Function block with 0 input ports and 2 output ports as specified.
There might be some other issue in the model that is causing this.
mehrzad
mehrzad 2025-2-13
Thanks @Aabha
Sorry for the late reply.
I noticed I was using th wrong block.
I appreaciate your reply thu.

请先登录,再进行评论。

采纳的回答

Aravind
Aravind 2025-2-13
From the image of your model, it seems you are using a “MATLAB Function” block rather than a “Level-2 MATLAB S-Function” block. The “MATLAB Function” block treats the code as a simple function, which means it automatically creates inports for all input variables, including “block.” Additionally, it does not create outports because the first function, “getThrust,” lacks output variables.
In contrast, “Level-2 MATLAB S-Function” blocks follow specific semantics, which allow them to handle initial variables like “block” differently, ignoring it as an input.
“Level-2 MATLAB S-Function” blocks are typically used for modeling dynamic states that cannot be handled by the “MATLAB Function” block. More details on the “MATLAB Function” block can be found here: https://www.mathworks.com/help/simulink/ug/what-is-a-matlab-function-block.html.
For a comparison between MATLAB Function and S-function, you can refer to this MATLAB Answer: https://www.mathworks.com/matlabcentral/answers/447451-difference-between-s-function-and-matlab-function.
To resolve your issue, transfer the code from the “MATLAB Function” block to a “Level-2 MATLAB S-Function” block. This change will give you a block with no inputs and two outputs, as you require.
I hope this helps address your issue.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by