Does Matlab have something similar to the main method?

If I have one Matlab source file (m file) containing a class definition ( classdef ), is there a way to specify a specific set of code that should be run if I run the m file? I mean the whole file, for example, using the Run button in the IDE, from the shell, or from the Matlab command line. I do not mean that you need to manually select the executable code.

A similar behavior exists in Java with the static main method, and in Python with code outside the class definition (possibly inside the if __name__==__main__ ).

+4
source share
2 answers

The short answer is no; MATLAB classdef M files are intended only for defining objects, and not for creating complete programs.

The long answer: you can get specific behavior from your classdef function if, for example, you overload the constructor to accept a flag that determines whether to act as a variable or act as a program.

eg.

 classdef myClass ... methods function self = myClass(varargin) if nargin == 1 && strcmpi(varargin{1},'run') ..... %run the program else ..... %make the variable 

OR you can create a static main method:

  methods (Static = true) function main() %enabes: myClass.main() ... end 

The IDE still does not know what to do with your M file to “run it,” but you can run it from the command line or another M file.

This last sentence is not 100% consistent - as Egon pointed out below, you CAN make the MATLAB IDE run this code - use the "launch configuration": http://www.mathworks.com/help/matlab/matlab_prog/run-functions-in- the-editor.html

+3
source

There are several ways to do this:

  • You can create a “ run configuration ” (either as a script or a specific line of code). This will be executed whenever you click the launch button (or click the launch shortcut) from your classdef file. The big drawback is that these startup configurations are stored locally, so this is a nightmare when it comes to collaboration or working in multiple places. Therefore, I would recommend writing a script if you have a complicated startup configuration. The mines are mostly called testMyClass , where MyClass , of course, the class you want to run.

  • If you do not need complex code, you can also put everything in the constructor of your object. If you are checking for arguments passed with if nargin == 0 ... end , this piece of code should be called whenever you run the class file. However, you are somewhat limited by what you can do, as you can create endless loops or an endless chain of objects to create if you are not careful. As a result, you will only have an object in the workspace of the database.

  • If you need more complex code or some code that makes some variables in the base workspace, this can be done, but it is expensive. Your code may end up in a complete mess, so I advise you not to use this unless you have a particularly good reason. You can use the previous method and the evil functions evalin and assignin to evaluate and assign variables in the base workspace.

+2
source

All Articles