Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.31 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is it possible to call a function that is not in the path in MATLAB?
  2. builtin('reshape', arg1, arg2, ..., argN);
  3.        
  4. function runExtended(script,varargin)
  5.  
  6.     cur = cd;
  7.  
  8.     if isempty(script), return, end
  9.     if ispc, script(script=='/')=''; end
  10.     [p,s,ext] = fileparts(script);
  11.     if ~isempty(p),
  12.         if exist(p,'dir'),
  13.             cd(p)
  14.             w = which(s);
  15.             if ~isempty(w),
  16.                 % Check to make sure everything matches
  17.                 [wp,ws,wext] = fileparts(w);
  18.                 % Allow users to choose the .m file and run a .p
  19.                 if strcmp(wext,'.p') && strcmp(ext,'.m'),
  20.                     wext = '.m';
  21.                 end
  22.  
  23.                 if ispc
  24.                     cont = ~strcmpi(wp,pwd) | ~strcmpi(ws,s) | ...
  25.                         (~isempty(ext) & ~strcmpi(wext,ext));
  26.                 else
  27.                     cont = ~isequal(wp,pwd) | ~isequal(ws,s) | ...
  28.                         (~isempty(ext) & ~isequal(wext,ext));
  29.                 end
  30.                 if cont
  31.                     if exist([s ext],'file')
  32.                         cd(cur)
  33.                         rehash;
  34.                         error('MATLAB:run:CannotExecute','Can''t run %s.',[s ext]);
  35.                     else
  36.                         cd(cur)
  37.                         rehash;
  38.                         error('MATLAB:run:FileNotFound','Can''t find %s.',[s ext]);
  39.                     end
  40.                 end
  41.                 try
  42.                     feval(s,varargin{:});
  43.                     %           evalin('caller', [s ';']);
  44.                 catch e
  45.                     cd(cur);
  46.                     rethrow(e);
  47.                 end
  48.             else
  49.                 cd(cur)
  50.                 rehash;
  51.                 error('MATLAB:run:FileNotFound','%s not found.',script)
  52.             end
  53.             cd(cur)
  54.             rehash;
  55.         else
  56.             error('MATLAB:run:FileNotFound','%s not found.',script)
  57.         end
  58.     else
  59.         if exist(script,'file')
  60.             evalin('caller',[script ';']);
  61.         else
  62.             error('MATLAB:run:FileNotFound','%s not found.',script)
  63.         end
  64.     end
  65.  
  66. end
  67.        
  68. libpath = '/home/user/mylib';
  69. % move mylib to the end of the path
  70. addpath(libpath, '-end');
  71. % now call some built-in functions that mylib overwrites
  72. reshape(rand(100),10,10);
  73. % return mylib to the top
  74. addpath(libpath)