Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function that is not on the Matlab path WITHOUT ADDING THAT PATH

I have been searching an entire afternoon and have found no solution to call in matlab a function by specifying its path and not adding its directory to the path.

This question is quite similar to Is it possible to call a function that is not in the path in MATLAB?, but in my case, I do not want to call a built-in function, but just a normal function as defined in an m-file.

I think handles might be a solution (because apparently they can refer to functions not on the path), but I again found no way to create a handle without cd-ing to the directory, creating it there and the cd-ing back. Trying to 'explore' what a function handle object is and how to make one with a reference to a specific function not on the path has led me nowhere.

So the solution might come from two angles:
1) You know how to create a handle for an m-file in a specific directory.
2) You know a way to call a function not on the matlab path.

EDIT: I have just discovered the function functions(myhandle) which actually lets you see the filepath to which the handle is referring. But still no way to modify it though...

like image 221
reverse_engineer Avatar asked Oct 25 '12 15:10

reverse_engineer


People also ask

How do I run a function in another directory in MATLAB?

Use addpath() to add the other directory to the MATLAB path.

How do I remove something from a path in MATLAB?

Description. rmpath( folderName ) removes the specified folder from the search path.

Where does MATLAB search for the data file if the folder path is not provided in filename?

When you do not specify a path to a file, MATLAB® looks for the file in the current folder or on the search path. Functions in the current folder take precedence over functions with the same file name that reside anywhere on the search path. To identify the current folder, type pwd in the Command Window.


2 Answers

This is doable, but requires a bit of parsing, and a call to evalin.

I added (many years ago!) a function to the MATLAB Central File Exchange called externalFcn

http://www.mathworks.com/matlabcentral/fileexchange/4361-externalfcn

that manages calls to off-path functions. For instance, I have a function called offpathFcn that simply returns a structure with a success message, and the value of an input. Storing that function off my MATLAB path, I can call it using:

externalfcn('out = C:\MFILES_OffPath\offpathFcn(''this is a test'')');

This returns:

out = 
    success: 1
    input: 'this is a test'

(Note that my implementation is limited, and improvable; you have to include an output with an equal sign for this to work. But it should show you how to achieve what you want.)

(MathWorks application engineer)

like image 193
Brett Shoelson Avatar answered Sep 21 '22 18:09

Brett Shoelson


The solution as noted in the comment 1 to create a function handle before calling the function is nicely implemented by @Rody Oldenhuis' FEX Contribution: http://www.mathworks.com/matlabcentral/fileexchange/45941-constructor-for-functionhandles

like image 27
Thierry Dalon Avatar answered Sep 23 '22 18:09

Thierry Dalon