using a function as the "main" script
Show older comments
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.
9 Comments
dpb
on 26 Dec 2014
Scope of functions is somewhat similar to file scope in C.
You can have multiple functions in the same file in Matlab, simply that they're not visible outside that function file.
Whether that is or is not advantageous depends on the purpose of the function--obviously, if it's a general new utility function of broad potential application it makes no sense to make it only visible within some other function. OTOH, if it's a helper function or simply a factorization of a functional objective, then it's far better that it be such.
"When in Rome..." is worth remembering--Matlab is not C (or Fortran or any other particular language). It is somewhat unique and one eventually becomes used to those peculiarities albeit some are easier to adjust to than others depending on one's background.
John D'Errico
on 26 Dec 2014
So you need to learn different programming patterns. If all languages were the same, then why bother with another language?
We can't really know what other "factors" you would want to consider, since we can't know what things you will find annoying.
Josh
on 27 Dec 2014
John D'Errico
on 27 Dec 2014
And we don't know what you might consider a downside. Perhaps it is time to just learn MATLAB instead of grousing about things being different from C.
dpb
on 27 Dec 2014
I thought I outlined a fair reason for/against choosing to do that (incorporate multiple functions within a given file, that is).
The question of whether a "main" is written as a function or a script is primarily a choice of what one wants for scope and for what purpose the script is being written--and we can't know that for you plus it's highly dependent on a given task. Sometimes you want stuff to be in the workspace because you're working interactively; other times there's no need to have anything visible in the workspace so functions are a logical choice to hide clutter.
To me the question is almost entirely scope and the purpose of whether I'm writing code "in anger" (as a Scottish power plant engineer I worked with years ago referred to plant testing once the preliminary work/setup was done) or whether it's just a throwaway for a query here or on cs-sm or the like.
Josh
on 28 Dec 2014
Image Analyst
on 28 Dec 2014
dpb, please put/move extensive comments like this as answers so I can vote for them. I'm trying to get you (and Greg) up to 3000 points by the end of the year so you can have full "god-like" powers.
Josh
on 29 Dec 2014
dpb
on 29 Dec 2014
OK, IA, I did...I had noticed you were being pretty aggressive on voting; wasn't sure of your motive(s)... :)
Accepted Answer
More Answers (2)
It's always changing with each release, but the one area that may make some difference is the level of optimization done by the JIT compiler on scripts vis a vis functions. At one time, at least, scripts weren't or were only moderately optimized whereas functions were (iirc; this is memory from quite a while ago, not gospel). I believe at current time only difference of real significance is that between the interactive command line as compared to m-files whether they be functions or scripts.
The one obvious difference is that there's a slight additional overhead of calling a function to build the local scope but for a main only called once, that should be negligible.
The key difference from C or other compiled languages is the introduction of the interpreter to eliminate the specific compile/link step. With the advent of the hidden JIT compiler while was always a step there to get from source to an executable form, the performance has improved significantly in most areas at the cost of the increased number of methods and higher-level data abstractions that have noticeably higher overhead associated with them. It's a tradeoff that only "time-in-grade" will eventually let you evaluate more fully in comparison to the alternatives.
In general, one finds the added functionality of the packaged supporting libraries make up for the overhead; otherwise there wouldn't continue to be a market. At some point, the complexity and/or computational intensity of projects can get to the point that it becomes necessary to move some or all of the computational engine to compiled code but often that can be done simply by judicious use of mex-files and the like while retaining the user interface.
ADDENDUM
On Sean's comment -- there's always while debugging scripts the alternative besides setting the breakpoint in the function of using assignin to place variables of interest in the workspace. I've at least been known to do this for this reason, particularly if there are other corollary things there that aren't in the function I'm playing with at the time...
Sean de Wolski
on 29 Dec 2014
Edited: Sean de Wolski
on 29 Dec 2014
0 votes
I use scripts for simple one time things and for examples that I want to publish, or examples that document the use of a function. Everything else I write is a function or class.
- Drawbacks to functions: you have to put a breakpoint at the end to see its workspace. I do this so much I barely consider it to be a drawback.
- Drawbacks to scripts: they mess with and possibly depend on your base workspace. They don't clean up after themselves. They're not quite as jittable because the interpreter has no idea what it's going to be given until runtime.
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!