How can I pack several .m ?

2 ビュー (過去 30 日間)
Belen
Belen 2013 年 10 月 28 日
回答済み: Jan 2013 年 10 月 28 日
I want to share several ".m" created to run a main function. This main function calls all these ".m" in different order depending on user needs. Is there any way to pack or compile all ".m" in one file? Preferably, this output file can not be modify by users.

回答 (2 件)

Jan
Jan 2013 年 10 月 28 日
What's wrong with having multiple M-files? An option would be to move all of them into a subfolder called \private\, such that only the main fail in the parent folder can access them.
If you want to have them in one M-file, you need a so called wrapper:
function varargout = MyWrapper(Func, varargin)
varargout = cell(1, nargout);
switch Func
case 'SubFunction1'
varargout = SubFunc1(varargin{:});
...
otherwise
error('Unknown function: %s', Func)
end
function [a,b] = SubFunc1(c, d)
...
Another method is to offer a set of function handles:
function FcnList = MyWrapper
FcnList.Function1 = @Function1;
FcnList.Function2 = @Function2;
...
function [a,b] = Function1(c, d)
...
function [q,p] = Function2(z)
...
Now you obtainthe handles in the first step inside the main function, and call the function handles afterwards:
FcnList = MyWrapper;
[a,b] = FcnList.Function1(c, d);
...

Vishal Rane
Vishal Rane 2013 年 10 月 28 日
Have you thought about implementing the other functions as subfunctions in the main function file?
  1 件のコメント
Belen
Belen 2013 年 10 月 28 日
First of all, thanks for your quick reply. Yes I've thought about it, but I use this main function file to generate a report (using publish), so, I prefer not to add all these subfunctions to the main file,in order to get a clean final report. Any other suggestion?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAdding custom doc についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by