How to programmatically test how many time a Simulink function is called in Simulink function?

8 visualizaciones (últimos 30 días)
In simulink, I'm using test harness to test a matlab funciton block which has matlab function call within the code. However, beside able to see the function call occurance and parameter in sequence viewer, I have not find a way to monitor event through command. The reason for this need is that I want to create a test sequence that verify if at certain moment and function is called correctly.

Respuesta aceptada

Shubham
Shubham el 19 de Oct. de 2024
Hey Wei,
I understand that you want to track the number of function calls being made to check if the function is being called correctly by creating a test sequence.
To programmatically test how many times a Simulink function is called within a MATLAB function block, you can do the following:
  • Modify your MATLAB function to include a persistent counter that increments each time the function is called.
function y = myFunction(u)
persistent callCount;
if isempty(callCount)
callCount = 0;
end
callCount = callCount + 1;
y = u; % Your function logic here
assignin('base', 'callCount', callCount); % Save the count to the base workspace
end
  • The persistent keyword in MATLAB allows the variable callCount to retain its value between function calls.
  • Also, the assignin function is used to save the desired value into base workspace.
  • Use a MATLAB script to run the simulation and check the value of the counter after the simulation completes. For this purpose, you can use the evalin function:
% Load the model
load_system('your_model');
% Run the simulation
sim('your_model');
% Retrieve the call count from the base workspace
callCount = evalin('base', 'callCount');
disp(['The function was called ', num2str(callCount), ' times.']);
  • Once you have the count of function calls made in your base workspace, you can create a test sequence and assert if the call count matches the expected value.
I hope this would help!
  1 comentario
Wei
Wei el 21 de Oct. de 2024
Hi Shubham,
Thank you for your quick reply, this is one solution to track all function for how many time they are called. However, the issue with this approach is that I will have to add this count function to every function that I would want to track, which is fine for simulation and testing purpose, but it will have to be removed later when the model need generate code during deployment. I was hoping if there is a way to track function call inside the test sequence block so that I can make use the 'verify' command, which flexibility of know the exact time of function being called, similar to the result of sequence viewer.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by