Consider using the 'Visible' - 'off' property to stop displaying the figure.
fig = figure('Visible', 'off');
This way you can stop displaying the image for every frame.
However, the delay in execution isn't primarily due to the figure being displayed each time. The real issue is the creation and updating of the figure for each frame. You can speed up the process by moving 'figure(1)' outside the loop to avoid creating new figures repeatedly. Additionally, consider these optimization techniques to enhance performance:
- Ensuring that arrays or matrices used in your loop are preallocated. This can prevent MATLAB from reallocating memory during each iteration.
- Using the 'getframe' function efficiently by capturing only the necessary parts of the figure. Leverage the following documentation to learn more about the ‘getframe’ function: https://www.mathworks.com/help/matlab/ref/getframe.html
- Updating properties of the figure using the 'set' function.
For more information on how to write efficient code, refer to the following documentation:
I've created a sample MATLAB script with dummy data to demonstrate execution times for different scenarios, which is attached for your reference.
Refer the image below to better understand the execution times for all three cases.
The results clearly show that simply disabling figure visibility (Case 1 and Case 2) doesn't significantly improve execution time. However, by implementing the suggested techniques, there's a notable reduction in execution time (Case 3).
I hope this resolves your issue.