7

In MATLAB I can define multiple functions in one file, with only the first defined function being visible external to that file. Alternatively, I can put each function in its own file and make them all globally visible through the path. I'm writing a menu driven application, where each menu item runs a different function. Currently, these are all in one big file, which is getting increasingly difficult to navigate. What I'd like to do is put groups of related functions into separate files.

I think I can do something like this by putting all the child functions into a separate directory and then adding the directory to the path in my parent function, but this feels a bit messy and inelegant.

Can anyone make a better suggestion?

Note: I'm most familiar with MATLAB 2006, but I'm in the process of upgrading to MATLAB 2009.

gnovice
  • 125,304
  • 15
  • 256
  • 359
Ian Hopkinson
  • 3,412
  • 4
  • 24
  • 28
  • Related : http://stackoverflow.com/questions/8883499/can-i-import-a-namespace-globally-without-explicitly-calling-import-in-each-and?lq=1 – Andrey Rubshtein Jan 06 '15 at 17:30

3 Answers3

16

One suggestion, which would avoid having to modify the MATLAB path, is to use a private function directory. For example:

Let's say you have a function called test.m in the directory \MATLAB\temp\ (which is already on the MATLAB path). If there are local functions in test.m that you want to place in their own m-files, and you only want test.m to have access to them, you would first create a subdirectory in \MATLAB\temp\ called private. Then, put the individual local function m-files from test.m in this private subdirectory.

The private subdirectory doesn't need to be added to the MATLAB path (in fact, it shouldn't be added to the path for things to work properly). Only the file test.m and other m-files in the directory immediately above the private subdirectory have access to the functions it contains. Using private functions, you can effectively emulate the behavior of local functions (i.e. limited scope, function overloading, etc.) without having to put all the functions in the same m-file (which can get very big for some applications).

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • I didn't know you could do this. I'll give it a go to see how it works out. At least all the function files will be hidden in a quiet corner! – Ian Hopkinson Aug 17 '09 at 10:53
  • 1
    It looks like the docs linked to here have been removed! Are private function directories considered obsolete? – Dan Oct 14 '14 at 06:38
  • 1
    @Dan: No, not obsolete. Just an old, bad link since TMW updated their online docs. I've edited in the new links. – gnovice Oct 14 '14 at 12:45
5

Maybe something like this,

function foobar
    addpath C:\Include\ModuleX

    %% Script file residing in ModuleX
    some_func();
end

Of course, ModuleX will remain in your search path after exiting foobar. If you want to set it to the default path without restarting, then add this line:

path(pathdef)

See ADDPATH for more details.

gnovice
  • 125,304
  • 15
  • 256
  • 359
Jacob
  • 34,255
  • 14
  • 110
  • 165
  • +1 because this is a reasonable solution which I'd considered. – Ian Hopkinson Aug 17 '09 at 10:54
  • In case the files are in a folder hierarchy, you can also add all of them with `addpath(genpath(PATH));`. Note that the `genpath`command is usually very slow. – tashuhka Oct 14 '14 at 13:59
1

You can use sub-folders that begin with "+" to separate functions into namespaces.

For example:

Place a function "bar" in the folder "+foo"

function bar()
print('hello world');

This function can be used as:

foo.bar() % prints hello world

More information can be found here:

What is the closest thing MATLAB has to namespaces?

Community
  • 1
  • 1