Create a library of functions hidden from the path
Suppose I have written 40 functions and I don't want 40 m-files but I want to put them in 1 big file. Suppose I want to use the functions from this big file in another function. So something like a library with functions that you want to access from a m-file.A hack which does exactly what you want but is probably quite
inefficient .... The code allows you to *import* sub/nested
function contained within a single m-file into the current
workspace without having to access them from a structure.
% M_IMPORT_TEST % % A m file function library compatible with % m_import. % % Usage % % m_import m_import_test % whos % See the new functions in the workspace % a = fun1(); % Use fun1 % b = fun2(); % Use fun2 % c = fun3(); % Use fun3 function lib = m_import_test lib = { @fun1 @fun2 @fun3 }; function fun1 disp('1'); end function fun2 disp('2'); end function fun3 disp('3'); end end
% M_IMPORT % % import mfiles from a library into the current workspace. % % Arguments % fname - The name of the file where the library lives % % The library function must return a cell array of function % handles. This function then inserts into the *callers* % workspace a set of function handles as variables with the % same name as the original functions. % function m_import(fname) funs = feval(fname); for i = 1:length(funs) % Strip of the function prefix [pathstr,name,ext,versn] = fileparts(func2str(funs{i})); % Place the function in the callers workspace assignin('caller', name, funs{i}); end end