0

How to call an m file from another m file in MATLAB without add its folder path? I don't want to add its folder via

addpath(genpath(''))
  • 2
    can you give a good reason why you don't want to add the path? - Your alternative is to copy and paste that .m file to your working directory (which is somewhat silly). – GameOfThrows Feb 02 '16 at 11:49
  • I want a folder that have multiple .m file. I don't want use another m file in that folder in my main function. also I can not use copy because of duplication. – user5872407 Feb 02 '16 at 11:57
  • Well, as @GameOfThrows said, there's two options when it comes to calling .m-files: copy the file to your current working directory *or* use `addpath`. As far as I know there isn't another option to do this, even a call to the `fullpath` of the file and then trying to `eval` or `feval` doesn't work. – Adriaan Feb 02 '16 at 11:59
  • 1
    That is the idea of a working directory right? You can have your main function in say file:///home/project/ ; and your function codes in file:///home/project/functions? – GameOfThrows Feb 02 '16 at 12:01
  • I have a big project. I have a folder with accessory files and folder for main function. some accessory files customize for my main function and another some are fix. because of this, I want to use certain accessory file in my main function – user5872407 Feb 02 '16 at 12:07
  • If it is a function you need to `cd` into the path or add the path to your MATLAB path. If it is a script you can do the above or use [`run`](http://www.mathworks.com/help/matlab/ref/run.html) – sco1 Feb 02 '16 at 12:11
  • @excaza: That's true, but then again all functions are visible. I understood the second comment in a way that only one function should be visible. Using cd is probably the standard way to expose such functions for unit tests, at least I use [this code](http://stackoverflow.com/questions/24612002/how-to-isolate-unittests-in-matlab/24616475#24616475) – Daniel Feb 02 '16 at 12:13
  • 1
    @Daniel yes, but it's the only way to accomplish what he is asking. If he wants to call a specific file then he can write a small wrapper function, pass the full path and any inputs, call the function, then return to the previous directory. This is all `run` does (minus inputs because it's scripts only). – sco1 Feb 02 '16 at 12:16

2 Answers2

3

Similar to the functionality of MATLAB's built-in run for scripts, you can cd into the secondary path, execute your function, and then return to the previous directory.

A small example:

% testcode.m
function [output] = testcode(fullfunctionpath, A, B)
[pathname, functionName] = fileparts(fullfunctionpath);
olddir = cd(pathname);

output = feval(functionName, A, B);
cd(olddir);
end

% .\test\testing.m
function [output] = testing(A, B)
output = A + B;
end

With the call:

C = testcode('C:\testcode-matlab\test\testing.m', 1, 2);

Will return

C =

     3

Note that this approach does not have error handling nor does it check for duplicates that already exist in your path definition. If the called function is not present in the target folder but does exist in the path, the function in MATLAB's path will still be executed. See: Function Precedence Order for more information.

sco1
  • 12,154
  • 5
  • 26
  • 48
1

All visibility rules for functions are based on Folders. If you want to have different visibilities you have to place your functions in different folders.

Typically, avoiding duplicate function names and just adding all source files to your search path is what works. To avoid duplicate function names you may want to take a look at packages.

Daniel
  • 36,610
  • 3
  • 36
  • 69