开发者

Load Multiple Functions from a Single File in Matlab [duplicate]

开发者 https://www.devze.com 2023-04-08 05:52 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Is it possible to define more than one function per file in MATLAB?
This question already has answers here: Closed 11 years ago.

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 trie开发者_高级运维d 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消