FEVAL_W_PATH Calls a function located by the specified path. FEVAL_W_PATH(FPATH, F, x1, ..., xn) [y1, ..., yn] = FEVAL_W_PATH(FPATH, F, x1, ..., xn) Identical to Matlab's own FEVAL, except that the function F need not be in the Matlab path if it is defined in a file in the path specified by FPATH. Assumes that the current working directory is always first in the Matlab path. Inputs: FPATH - string containing the path to the function to be called, can be absolute or relative to current working directory F - string containing the name of the function to be called x1, ..., xn - variable number of input arguments to be passed to F Output: y1, ..., yn - variable number arguments returned by F (depending on the caller) Note that any sub-functions located in the directory specified by FPATH will also be available to be called by the F function. Examples: % Assume '/opt/testfunctions' is NOT in the Matlab path, but % /opt/testfunctions/mytestfcn.m defines the function mytestfcn() % which takes 2 input arguments and outputs 1 return argument. y = feval_w_path('/opt/testfunctions', 'mytestfcn', x1, x2);
0001 function varargout = feval_w_path(fpath, fname, varargin) 0002 %FEVAL_W_PATH Calls a function located by the specified path. 0003 % FEVAL_W_PATH(FPATH, F, x1, ..., xn) 0004 % [y1, ..., yn] = FEVAL_W_PATH(FPATH, F, x1, ..., xn) 0005 % 0006 % Identical to Matlab's own FEVAL, except that the function F need not be 0007 % in the Matlab path if it is defined in a file in the path specified by 0008 % FPATH. Assumes that the current working directory is always first in 0009 % the Matlab path. 0010 % 0011 % Inputs: 0012 % FPATH - string containing the path to the function to be called, 0013 % can be absolute or relative to current working directory 0014 % F - string containing the name of the function to be called 0015 % x1, ..., xn - variable number of input arguments to be passed to F 0016 % 0017 % Output: 0018 % y1, ..., yn - variable number arguments returned by F (depending on 0019 % the caller) 0020 % 0021 % Note that any sub-functions located in the directory specified by FPATH 0022 % will also be available to be called by the F function. 0023 % 0024 % Examples: 0025 % % Assume '/opt/testfunctions' is NOT in the Matlab path, but 0026 % % /opt/testfunctions/mytestfcn.m defines the function mytestfcn() 0027 % % which takes 2 input arguments and outputs 1 return argument. 0028 % y = feval_w_path('/opt/testfunctions', 'mytestfcn', x1, x2); 0029 0030 % MATPOWER 0031 % Copyright (c) 2016, Power Systems Engineering Research Center (PSERC) 0032 % by Ray Zimmerman, PSERC Cornell 0033 % 0034 % This file is part of MATPOWER. 0035 % Covered by the 3-clause BSD License (see LICENSE file for details). 0036 % See http://www.pserc.cornell.edu/matpower/ for more info. 0037 0038 %% check input type 0039 if ~ischar(fpath) 0040 error('feval_w_path: FPATH must be a string'); 0041 end 0042 if ~ischar(fname) 0043 error('feval_w_path: FNAME must be a string'); 0044 end 0045 0046 %% see if path exists 0047 if exist(fpath, 'dir') ~= 7 0048 error('feval_w_path: Sorry, ''%s'' is not a valid directory path.', fpath); 0049 end 0050 0051 cwd = pwd; %% save the current working dir 0052 cd(fpath); %% switch to the dir with the mfile 0053 try 0054 [varargout{1:nargout}] = feval(fname, varargin{:}); 0055 cd(cwd); %% switch back to saved dir 0056 catch 0057 cd(cwd); %% switch back to saved dir 0058 rethrow(lasterror); 0059 end