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 で Manage Products についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!