在 Matlab 中从单个文件加载多个函数

发布于 2024-12-07 05:47:10 字数 1089 浏览 1 评论 0原文

可能的重复:
是否可以定义更多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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2024-12-14 05:47:10

恐怕这是不可能的,每个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文