Based on my understanding, you are looking to clock the total time taken for the output of the function fcn() ,’y’, to change from 1 to 0. I am assuming that there is an external function that is capturing the data from the ultrasonic sensor and storing it in the input variable ‘u’. Since the code calling the function 'fcn()' is not shared in the question, I am assuming that the function is being called within a loop with unique ‘u’ values at every iteration.
To calculate the total time taken for variable ‘y’ to move from 1 to 0, we can use the ‘tic’ and ‘toc’ function. Refer to the sample code attached below for an example of how to use these functions to determine the total execution time. Please note that I am assuming ‘u’ to be populated by the function ‘readUltrasonicSensor()’.
prev_y = fcn(readUltrasonicSensor());
startTime = tic;
while true
u = readUltrasonicSensor();
y = fcn(u);
if prev_y == 1 && y == 0
elapsedTime = toc(startTime); % Measure time since start
fprintf('Time elapsed from start to output change (1 to 0): %f seconds\n', elapsedTime);
break;
end
prev_y = y;
pause(0.01);
end
Additionally, you can refer to the following documentation link for more information:
- https://www.mathworks.com/help/matlab/ref/tic.html
- https://www.mathworks.com/help/matlab/ref/toc.html
Hope this helps!