在 Matlab 中从单个文件加载多个函数
是否可以从 Matlab 中的同一个 .m 文件加载多个函数?我发现为许多小型别名实用程序函数的每个函数创建一个文件很麻烦。我已经尝试过这个技巧,它允许Octave,但不允许我的Matlab。我收到以下错误:
??? Error: File: /home/per/Documents/MATLAB/aliases.m Line: 6 Column: 1
Function definitions are not permitted in this context.
我的 aliases.m
文件当前包含
% Prevent Octave from thinking that this
% is a function file:
1;
function y = isvariable(x)
%Return non-zero if x is a function.
y = exist(x, 'var');
end
function y = isfile(x)
%Return non-zero if x is a function.
y = exist(x, 'file');
end
function y = isdir(x)
%Return non-zero if x is a function.
y = exist(x, 'dir');
end
function y = isbuiltin(x)
%Return non-zero if x is a function.
y = exist(x) == 5;
end
Possible Duplicate:
Is it possible to define more than one function per file in MATLAB?
Is it possible to load multiple functions from the same .m file in Matlab? I find it cumbersome to create a single file for each function for many small alias utility functions. I have already tried this tip which is allowed Octave, but not in my Matlab. I get the following error:
??? Error: File: /home/per/Documents/MATLAB/aliases.m Line: 6 Column: 1
Function definitions are not permitted in this context.
My aliases.m
file currently contains
% Prevent Octave from thinking that this
% is a function file:
1;
function y = isvariable(x)
%Return non-zero if x is a function.
y = exist(x, 'var');
end
function y = isfile(x)
%Return non-zero if x is a function.
y = exist(x, 'file');
end
function y = isdir(x)
%Return non-zero if x is a function.
y = exist(x, 'dir');
end
function y = isbuiltin(x)
%Return non-zero if x is a function.
y = exist(x) == 5;
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕这是不可能的,每个 m 文件只包含一个 MATLAB 函数(您可以有嵌套函数或子函数,但无法在文件外部访问它们)。
如果您担心在全局范围内放置太多内容,请考虑 OOP 和 命名空间。
I'm afraid that is not possible, each m-file contains exactly one MATLAB function (you can have nested or sub-functions, but they are not accessible outside of the file).
If you are concerned about putting too much stuff on the global scope, think about OOP and namespaces.