use .m file

6 次查看(过去 30 天)
Max Yaffe
Max Yaffe 2023-4-8
How do I use a .M file with two functions defined in the Command Window?

采纳的回答

Walter Roberson
Walter Roberson 2023-4-8
If a .m file is a script file (first executable word is not function or classdef ) then you cannot directly call any functions defined in the script file from outside of the .m file.
If a .m file is a function file (first executable word is function) then you can only directly call the first function in the file, and the name you invoke it by must be the same as the name of the file (the actual name given in the function line will be ignored in this case.)
If a .m file is a class definition, then you can only directly call the constructor and public static methods, unless you are passing in a member of the class in which case you can call public methods.
If you need to be able to call additional functions inside a script file or inside a function file, then the only way to do so if you have somehow been provided with a handle to the function.
H = get_impl();
H{2}()
setter invoked
function H = get_impl()
H = {@my_getter, @my_setter};
end
function my_getter()
disp('getter invoked');
end
function my_setter()
disp('setter invoked')
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by