If I have a single Matlab source file (m-file) containing a class definition (classdef
), is there any way to specify a particular set of code that is to be executed if I run the m-file? I mean the entire file, such as via the Run
button in the IDE, from a shell, or from the Matlab command-line. I don't mean manually selecting the code to be executed.
Similar behaviour exists in Java with the static main
method and in Python by having code outside the class definition (possibly inside a if __name__==__main__
block).
The short answer is "no"; MATLAB classdef M-files are just intended to define objects, not form complete programs.
The long answer is you might be able to get specific behavior out of your classdef function if, for example, you overload the constructor to take a flag specifying whether or not to "act like a variable" or "act like a program".
e.g.
classdef myClass
...
methods
function self = myClass(varargin)
if nargin == 1 && strcmpi(varargin{1},'run')
..... %run the program
else
..... %make the variable
OR you can make a static method called main:
methods (Static = true)
function main()
%enabes: myClass.main()
...
end
The IDE still won't know what to do with your M-file to "run it", but you can run it properly from the command line, or another M-file.
That last sentence is not 100% correct - as Egon pointed out below, you CAN make MATLAB's IDE run that code - use a "run configuration": http://www.mathworks.com/help/matlab/matlab_prog/run-functions-in-the-editor.html
There are a few ways you can do this:
You can create a "run configuration" (either as a script or a specific line of code). This will run whenever you click the run button (or press the run shortcut) from within your classdef
file. The big drawback is that those run configurations are stored locally, so this is a nightmare when it comes to collaboration or working at multiple places. So personally, I'd recommend writing a script if you have a complicated run configuration. Mine are mostly called testMyClass
where MyClass
of course is the class you want to run.
If you don't require complicated code, you can also put everything in the Constructor of your object. If you check whether there are no arguments passed with if nargin == 0 ... end
, that piece of code should be called whenever you 'run' the class file. However, you are somewhat limited in what you can do, as you could create infinite loops or an infinite chain of those objects being created if you are not careful. In the end, you will have just the object in your base workspace.
If you do require more complicated code or some code that makes some variables within the base workspace, it can be accomplished but it comes at great cost. Your code might end up a complete mess, so I advise against using this unless you have an extremely good reason otherwise. You can use the previous method and the evil functions evalin
and assignin
to evaluate and assign to variables in the base workspace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With