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 the built-in FEVAL, except that the function F need not be in the MATLAB/Octave 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/Octave 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/Octave 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 the built-in FEVAL, except that the function F need not be 0007 % in the MATLAB/Octave path if it is defined in a file in the path specified 0008 % by FPATH. Assumes that the current working directory is always first in 0009 % the MATLAB/Octave 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/Octave 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 https://matpower.org 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 if isempty(fpath) %% just call feval directly, no cd necessary 0047 [varargout{1:nargout}] = feval(fname, varargin{:}); 0048 else 0049 %% see if path exists 0050 if exist(fpath, 'dir') ~= 7 0051 error('feval_w_path: Sorry, ''%s'' is not a valid directory path.', fpath); 0052 end 0053 0054 cwd = pwd; %% save the current working dir 0055 cd(fpath); %% switch to the dir with the mfile 0056 try 0057 [varargout{1:nargout}] = feval(fname, varargin{:}); 0058 cd(cwd); %% switch back to saved dir 0059 catch 0060 cd(cwd); %% switch back to saved dir 0061 rethrow(lasterror); 0062 end 0063 end