How to create a Matlab module

Hi I'm trying to find out how to create a module in Matlab, that can be called using module.include() However I'm surprised to find out that I can't seem to find any official documentation about it. When I search in in mathworks.com, in the documentation, the first hit is even about Python modules ... (see screen shot). Does anyone know how to find this information? Thanks!

답변 (4개)

Rajesh Balagam
Rajesh Balagam 2018년 5월 3일

2 개 추천

If you are referring to equivalent of Python modules in MATLAB, you can use packages. Refer to the following MATLAB documentation page for more information:

댓글 수: 4

B Verhaar
B Verhaar 2018년 5월 7일
편집: B Verhaar 2018년 5월 7일
Thanks for your reply. No, I want to use Matlab modules. Using the commands module.include(), module.remove()
My point is that I cannot seem to find any documentation about this.
Walter Roberson
Walter Roberson 2018년 5월 7일
I see no evidence that such a thing exists in MATLAB, or in any MATLAB related package anywhere on the Internet. Where did you see such a thing in connection with MATLAB?
B Verhaar
B Verhaar 2018년 5월 7일
Hi Walter
The evidence you requested, you can obtain by typing "help module" in the command window.
I would expect to find more extensive info (e.g. with an introduction to modules) somewhere in the Matlab documentation.
Walter Roberson
Walter Roberson 2018년 5월 7일
편집: Walter Roberson 2018년 5월 7일
>> help module
module not found.
Use the Help browser search field to search the documentation, or
type "help help" for help command options, such as help for methods.
I do not have all of the toolboxes installed, but I have quite a number of them.
What shows up for
which -all module

댓글을 달려면 로그인하십시오.

Peeyush Prasad
Peeyush Prasad 2018년 11월 26일
이동: Walter Roberson 2023년 1월 9일

1 개 추천

Hi B.Verhaar,
After quite some head-scratching, I realized that 'Modules' is supplemental software(I think it is a toolbox) to manage the matlab search path. It needs to be installed on top of your regular Matlab installation, not sure if it comes in a 'full' installation. After installation, type 'Modules' on the cmdline, and a GUI pops open with all installed modules. Clicking on the 'Help' button reveals that it needs to be installed (see screenshot).matlab_modules.PNG
Also, 'which' returns the following:
>> which -all modules
C:\Users\peprasad.ASML-COM\AppData\Roaming\MathWorks\MATLAB\ModulesCache\732E09B121C82C10\R2018_10_02\Modules\Modules.m
so from the modules cache.
Hope that helps,
--Peeyush
José Crespo Barrios
José Crespo Barrios 2019년 7월 12일
편집: José Crespo Barrios 2019년 7월 12일

1 개 추천

The fast roundabout I make to create a python-like module in matlab is to define a module-function with only two arguments: 1) str_function (string with the function inside the module to be called), and 2) cell_args (cell with the input arguments for that function). "resp" is the response to be exported by the module-function, and the functions inside it.
function resp = module_LoL(str_function, cell_args)
if strcmp(str_function,'myfun_1')
resp = myfun_1(cell_args);
elseif strcmp(str_function,'myfun_2')
resp = myfun_2(cell_args);
elseif strcmp(str_function,'myfun_3')
resp = myfun_3(cell_args);
end %endif str_function
end %endmod module_LoL
% functions inside module -------------------
function resp = myfun_1(cell_args)
% arithmetic mean
[input_1, input_2] = cell_args{:};
resp = (input_1 + input_2)/2;
end % endfun myfun_1
function resp = myfun_2(cell_args)
% geometric mean
[input_1, input_2] = cell_args{:};
resp = sqrt(input_1 * input_2);
end % endfun myfun_2
function resp = myfun_3(cell_args)
% arithmetic-geometric mean: agm
[a,g] = cell_args{:}; % [input_1,input_2]
for ii = 1:10 % 10 iterations
a = myfun_1({a,g});
g = myfun_2({a,g});
end %endfor
resp = a;
end % endfun myfun_3
So that I can use the functions of this module outside, just by calling the head function-module as it would be a normal function (as a matter of fact it does).
value_1 = 2;
value_2 = 3;
am = module_LoL('myfun_1', {value_1, value_2}) % arithmetic mean
gm = module_LoL('myfun_2', {value_1, value_2}) % geometric mean
agm= module_LoL('myfun_3', {value_1, value_2}) % arithmetic-geometric mean
Besides that, the aspect is very nice inside the editor, because you can inspect every function inside the module with a single click in the proper panel of the editor, as shown in the next image,

댓글 수: 3

Guillaume
Guillaume 2019년 7월 12일
편집: Guillaume 2019년 7월 12일
Not sure what the whole thread is about. Just a comment about your code. A bunch of if...elseif...elseif...end is usually better coded as a lookup table. It's a lot easier to maintain as well. Compare your current module_LoL function which will require adding elseif and mostly duplicating code each time a new function is added to:
function resp = module_LoL(str_function, cell_args)
allowed_functions = {'myfun_1', 'myfun_2', 'myfun_3';
@myfun_1, @myfun_2, @my_fun3};
[found, where] = ismember(str_function, allowed_functions(1, :)); %search 1st row of lookup table
if ~found
error('Function %s is not a valid function');
end
resp = allowed_functions{2, where}(cell_args); %use matching 2nd row of lookup table
end
If you want add a function, simply add it to allowed_functions. Nothing else need to change. As a bonus, the code also tell you if the supplied function is invalid.
José Crespo Barrios
José Crespo Barrios 2019년 7월 14일
Thank you for your contribution, very elegant. Just wanted to show didactically that it is no necessary to download special packages to have a kind of python-like module. It is just putting a head function and redirect to inner functions with "str_function" and "cell_args". Regards
ScubaNinjaDog
ScubaNinjaDog 2023년 1월 9일
Very Cool!

댓글을 달려면 로그인하십시오.

Steven Lord
Steven Lord 2018년 5월 7일

0 개 추천

I have access to a copy of the current release with all the products, and like Walter I cannot find any module function in them. I did a quick Google search and I found a submission on the File Exchange that may be what you're looking for. However, that doesn't have a function or method named "include" in it.

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품

질문:

2018년 5월 1일

이동:

2023년 1월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by