Runtime discoverable and runable functions
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(3 个)
Walter Roberson
2011-1-26
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.
0 个评论
Chris
2011-1-26
1 个评论
Walter Roberson
2011-1-26
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.
Chris
2011-1-27
2 个评论
Walter Roberson
2011-1-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!