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

佐手、 提交于 2019-12-17 18:35:38

问题


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...


回答1:


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




回答2:


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)




回答3:


function [varargout]=funeval(fun,varargin)
% INPUT:
% fun: (char) full path to function file
curdir=cd;
[fundir,funname]=fileparts(fun);
cd(fundir);
[varargout{1:nargout}] =feval(funname,varargin{:})
cd(curdir);



回答4:


I've modified Thierry Dalon's code to avoid the use of feval, which I always feel uncomfortable with. Note this still doesn't get around cd-ing to the directory in question, but well, it happens behind the scenes, so pretend it doesn't happen :-) Also note what Ben Voigt pointed out above: calls to helper functions off the path will fail.

function [varargout]=funeval(FunctionHandle, FunctionPath, varargin)
% INPUT:
% FunctionHandle: handle to the function to be called; eg @MyFunction
% FunctionPath: the path to that function
% varargin: the arguments to be passed to Myfunction
curdir=cd;
cd(FunctionPath)
[varargout{1:nargout}] = FunctionHandle(varargin{:});
cd(curdir);
end

and calling it would look like

Output = funeval(@MyFunction, 'c:\SomeDirOffMatlabsPath\', InputArgToMyFunc)



回答5:


The run command can run a script file from any directory, but it can't call a function (with input and output arguments).

Neither feval nor str2func permit directory information in the function string.

I suggest writing your own wrapper for str2func that:

  • saves the working directory
  • changes directory to the script directory
  • creates a function handle
  • restores the original working directory

Beware, however, that a handle to a function not in the path is likely to break, because the function will be unable to invoke any helper code stored in other files in its directory.



来源:https://stackoverflow.com/questions/13072470/call-a-function-that-is-not-on-the-matlab-path-without-adding-that-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!