How to send any type of parameters in update-function to output-function in simulink s-function?

1 次查看(过去 30 天)
I try to show 'ax' in output, so I send 'ax' to output by adding it to the state vector 'x'.
But, it is not a good idea when the size of 'ax' vector is increasing. It will increase computation time because dim. of state increases.
global 'ax' doesn't work at all.
any suggestions?
function sys=mdlUpdate(t,x,u)
dt = 0.01;
Td = u;
% Numerical Integration
x_prev = x(1);
x(1) = x(1) + Td*dt; % Euler integration
ax = (x(1)-x_prev)/dt;
x(2) = ax;
sys = [x(1) x(2)];
function sys=mdlOutputs(t,x,u)
ax = x(2);
sys = [x(1) ax];

回答(1 个)

Pavan Sahith
Pavan Sahith 2023-10-20
Hello,
I understand you are trying to pass a parameter ‘ax’ in the update function to output function of the Simulink S-function block.
As a workaround you can try “ssSetUserData and “ssGetUserData functions which uses the S-function’s user data.
Please refer to this sample code to understand the use of ssSetUserData” and “ssGetUserData function.
%In update-function you can use these
% Retrieve 'ax' from the userdata field
% ‘gcb’- get current block.
userdata = ssGetUserData(gcb); % Get user data
ax = userdata.ax; % Access 'ax' from the user data
% Update 'ax' in the user data
userdata.ax = ax; % Update 'ax' in user data
ssSetUserData(gcb, userdata); % Set user data
% sample output-function can be like this
function sys = mdlOutputs(t, x, u)
% Retrieve 'ax' from the userdata field
userdata = ssGetUserData(gcb); % Get user data
ax = userdata.ax; % Access 'ax' from the user data
sys = [x(1); ax];
end
Please refer to the MathWorks documentation links to know more about
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Block and Blockset Authoring 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by