MATLAB: Using a function as the “main” script

functions

The fact that every function (excluding anonymous functions which are very limited) has to reside on a different file is very annoying, maybe because I'm used to other programming languages like C. It occurred to me that by making my main script a function I have the option of having functions defined in the same file,while I can still have other functions saved in their own file either because they are big enough to justify it, or they are used by other modules, etc.
One drawback is obvious: the workspace is not available after the program finishes. OK, I can live with that. Are there other factors to consider (needless to say I'm rather new to MATLAB)? I guess it cannot be really bad since when using GUIDE this is what happens anyway.

Best Answer

It's not really a drawback. In fact you could make a case where having variables hanging around after a function has finished is a drawback. It's just all about scope and every programming language has times when certain variables are in scope (able to be seen) and certain variables that are not in scope.
As far as what other things are different with C, you can search for "differences with C" on the Mathworks site and find pages like this: http://www.mathworks.com/help/matlab/matlab_oop/matlab-vs-other-oo-languages.html