You might be able to run MATLAB with the -nodesktop or -nodisplay option.
The behaviour you describe happens when your code uses figure() to create new figures or figure(existing_handle) to activate a particular figure to make it the default figure for following graphics commands. It would be best if you rewrote your code to create figures and graphics objects less often, and to instead re-use graphics objects and to make sure to always parent your graphics operations https://www.mathworks.com/matlabcentral/answers/22208-show-figure#comment_48401 MATLAB is able to create some graphics without an attached graphics display, but it is common to need more than it is able to provide in that mode. Sometimes graphics can be rewritten in terms of inserting lines and shapes using computer vision toolbox to write into buffers, but that can be pretty inconvenient for curves or surfaces. But with the advice about always parenting and doing focus-stealing operations as little as possible, then you can often reduce the number of times that MATLAB pops up the graphics window.