Clear Filters
Clear Filters

Runtime discoverable and runable functions

4 views (last 30 days)
Chris
Chris on 26 Jan 2011
I am putting together a frame work application with the ability to be expanded by writing new modules. Is there a way in MATLAB to enable this without re-writing the framework code each time a new module is created?

Answers (3)

Walter Roberson
Walter Roberson on 26 Jan 2011
A standard method for this is to put the registration routine for all of the extensions in to a common folder, and then at launch time, use dir() to find its contents and run the routines. If multiple files are required for implementation, private directories whose name begin with '@' can be used.

Chris
Chris on 26 Jan 2011
@Walter, thanks for the reply. Unfortunately I don't understand the terminology you have used. I have written a GUI app and functions in m files, but I don't know what a registration routine is or if there is something special in the design of an 'extension'. Can you elaborate a little for me please?
  1 Comment
Walter Roberson
Walter Roberson on 26 Jan 2011
What you are asking to do is much the same as what is done in IE7 or Firefox to customize the behaviour of the program. As additional functionality is being added to the original program, the new modules "extend" the functionality of the original program, and thus are commonly called "extensions".
In order to be able to write new modules and not have to change the original code at all (not even to add a explicit single initialization call), each of the modules will need to have a common interface mechanism. What needs to happen at that point depends upon whether these modules all take exactly the same parameters, and whether they are all to appear under exactly the same menu, and upon some other factors.
For example, if the module provides an interface to a device, it may need to report back the routines (or function handle) needed to get device properties, set device properties, open the device, close the device, read from the device, write to the device, and perhaps "position" the device. The same set of basic functions is also suitable for working with device-like files such as compressed files.
If the modules are pure math routine that do not need to remember anything, and which use the same parameters, then you can skip a lot of this infrastructure. But you wouldn't normally do something -quite- that simple, as you would normally want to ask the module to report back the text that should appear for it in the menu, and perhaps a brief description.
Also, it is not uncommon even with pure math routines that the user might wish to use the routine more than once with similar parameters, and that computation time can be saved by calculating some things once and remembering them for the next time the call is made. That remembered data might or might not be shared with other modules. For example, it would not be uncommon for several different routines to need a covariance matrix; it is a waste to have each one calculate the matrix just for the sake of having a "stateless" interface to the module. Thus, it would not be uncommon for modules to have initialization routines and routines to close the module down as it will not be used again, possibly along with mechanisms to signal that particular inputs are the same as the last time the module was called.

Sign in to comment.


Chris
Chris on 27 Jan 2011
@Walter thank you, yes that is exactly what I am trying to do. I was planning to have each extension located in its own subdirectory to minimise conflicts. I guess my question from here is how do I implement this? I don't mean a full code implementation of modules, I believe I can manage that, what I am stuck at is the MATLAB instructions for certain tasks requisite for the task. For example, I can search and find the m files with no problem, but assuming that, for example, the init routines are not all called init but maybe init_chris or init_walter, how do I code MATLAB to run those inits given that I don't know their names at coding time? If this were a C library such as a DLL I would give the top level functions the same name in each DLL but MATLAB, for obvious reasons, has a problem with functions of the same name, always running the first one it finds on its path, hence my thought of using standard function names with an addendum. Then there is the path, do I add all the extension paths to the path, or do I add them for the duration of execution and then remove them again? Any help or insight you might have into this would be much appreciated.
  2 Comments
Walter Roberson
Walter Roberson on 27 Jan 2011
feval() can execute routines by name. The name you would need to use would be the name of the .m file (without the extension.)
I suggest you examine the code for Matlab's run() function to see how it deals with changing directories so that the function is on the path.
Chris
Chris on 28 Jan 2011
Thats brilliant, thank you

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!