2

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
Community
  • 1
  • 1
Nordlöw
  • 11,838
  • 10
  • 52
  • 99

1 Answers1

2

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.

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454