Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Calling functions in other directories / Organising large programs

Subject: Calling functions in other directories / Organising large programs

From: Tim

Date: 1 May, 2009 15:01:04

Message: 1 of 2

Hi, I'm writing a large program and would like to organise parts of it into different subdirectories. Is there a way to do this in matlab cleanly? Can you call functions in other directories? Ie something like:

'subdir1/subdir2/'a_function(1, 3, 3);

As far as I can see there are only these ways to do this:

1. Add the subdirectories to the path. This is suboptimal because you then have to remove them. And it just isn't "the right way".
2. cd to the directory. Better, but still doesn't feel very clean and you still need to cd ../..

Is there a better way?

Subject: Calling functions in other directories / Organising large programs

From: Yi Cao

Date: 1 May, 2009 16:05:05

Message: 2 of 2

"Tim " <t.hutt07@imperial.ac.uk> wrote in message <gtf2rg$5mo$1@fred.mathworks.com>...
> Hi, I'm writing a large program and would like to organise parts of it into different subdirectories. Is there a way to do this in matlab cleanly? Can you call functions in other directories? Ie something like:
>
> 'subdir1/subdir2/'a_function(1, 3, 3);
>
> As far as I can see there are only these ways to do this:
>
> 1. Add the subdirectories to the path. This is suboptimal because you then have to remove them. And it just isn't "the right way".
> 2. cd to the directory. Better, but still doesn't feel very clean and you still need to cd ../..
>
> Is there a better way?

1. You can use "addpath" and "rmpath" explicitly in your main program so tha right path will be used during the execution of the main program but will not affect other programs after it finishes.

2. You can use private subdirectory under the directory where the main program resides. All functions under the private subdirectory can only be called by the programs reside in the parent directory. However, in this way, you can have only one subdirectory.

HTH
Yi Cao

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us