MATLAB: Matlab still busy a long time after last line of script is executed

busyMATLABscript

Summary: Script runs fine on first execution, Matlab stays busy very long after a subsequent execution of the exactly same script.
I am learning Matlab and am running a simple script with a couple of for- and if-loops and very small matrices (<1000 entries).
After first starting up Matlab and running the script it takes about 2 seconds to finish the script and the variables appear in the workspace shortly after. This leads me to believe that the code that I have is somewhat reasonable (no crazy infinite loops or anything like that…).
Then, after clearing the workspace, I do another, subsequent run of the same script without changing anything, the script executes in about 0.5s which is what I would expect.
However, after the last line is executed (in my case it is "toc"), Matlab remains busy for a solid 1-2 minutes. Only then, after this long time, the variables appear in the workspace again.
During all of this, I am monitoring CPU and RAM usage, and it stays well below 50% so I assume that is not the problem. What am I missing?

Best Answer

This is a known issue with Live Editor.
See this potential solution.
Note that the solution also mentions a point I made in the comments under your question. Make sure your script does not contain the command, "clear all".