2012-02-09 94 views
12

我已經安裝了一個庫,它有一些與MATLAB相同的函數。通過安裝lib,我的意思是addpath。當我嘗試調用這些函數時,它會使用該庫的實現,但我想調用MATLAB實現。是否可以調用不在MATLAB路徑中的函數?

爲了使它更簡單:我怎樣才能指定哪個函數來調用,因爲我有這兩個函數的絕對地址?

我搜索了答案,但我沒有在網站上找到它。

+1

這個圖書館有多大,你會用它做多少工作?你多久想從代碼中調用它的函數?庫中是否有OO代碼? – 2012-02-09 15:34:22

回答

10

如果您重載任何MATLAB內置函數來處理特定的類,那麼MATLAB將始終調用該類型的重載函數。如果出於某種原因需要調用內置版本,則可以使用內置函數覆蓋通常的調用機制。表達

builtin('reshape', arg1, arg2, ..., argN); 

強制MATLAB的內置函數的調用,重塑,通過即使存在這個參數列表類的重載所示的參數。

http://www.mathworks.com/help/techdoc/matlab_prog/br65lhj-1.html

8

使用run,它可以讓你使用自己的功能,而不是內建的沒有將其添加到路徑。

從幫助中獲取:

運行腳本不是當前路徑上 語法

運行腳本名

正如@Cheery正確地說,它不能用於接受功能參數。但是,run.m是可修改的文件,所以我做了一個擴展版本,可以接受參數。它可以很容易地修改輸出參數。

function runExtended(script,varargin) 

    cur = cd; 

    if isempty(script), return, end 
    if ispc, script(script=='/')='\'; end 
    [p,s,ext] = fileparts(script); 
    if ~isempty(p), 
     if exist(p,'dir'), 
      cd(p) 
      w = which(s); 
      if ~isempty(w), 
       % Check to make sure everything matches 
       [wp,ws,wext] = fileparts(w); 
       % Allow users to choose the .m file and run a .p 
       if strcmp(wext,'.p') && strcmp(ext,'.m'), 
        wext = '.m'; 
       end 

       if ispc 
        cont = ~strcmpi(wp,pwd) | ~strcmpi(ws,s) | ... 
         (~isempty(ext) & ~strcmpi(wext,ext)); 
       else 
        cont = ~isequal(wp,pwd) | ~isequal(ws,s) | ... 
         (~isempty(ext) & ~isequal(wext,ext)); 
       end 
       if cont 
        if exist([s ext],'file') 
         cd(cur) 
         rehash; 
         error('MATLAB:run:CannotExecute','Can''t run %s.',[s ext]); 
        else 
         cd(cur) 
         rehash; 
         error('MATLAB:run:FileNotFound','Can''t find %s.',[s ext]); 
        end 
       end 
       try 
        feval(s,varargin{:}); 
        %   evalin('caller', [s ';']); 
       catch e 
        cd(cur); 
        rethrow(e); 
       end 
      else 
       cd(cur) 
       rehash; 
       error('MATLAB:run:FileNotFound','%s not found.',script) 
      end 
      cd(cur) 
      rehash; 
     else 
      error('MATLAB:run:FileNotFound','%s not found.',script) 
     end 
    else 
     if exist(script,'file') 
      evalin('caller',[script ';']); 
     else 
      error('MATLAB:run:FileNotFound','%s not found.',script) 
     end 
    end 

end 
+1

它是爲SCRIPTS,而不是功能!你嘗試過嗎?你將無法以這種方式提供函數參數。這個函數文件調用的結果就像這樣'???輸入參數「x」未定義。其中「x」是函數的參數。 – Cheery 2012-02-09 18:10:47

+0

當然。試試這個函數文件函數y = myfunc(x)(新行在這裏) y = x; disp(y);'將其另存爲'myfunc.m'並在Matlab的命令行中嘗試'run path/myfunc.m'。結果將是'???輸入參數「x」未定義。Matlab有兩種類型的文件 - 用於函數和腳本。您不能直接從編輯器或命令行運行功能文件。函數文件應該在路徑中,並且Matlab將嘗試在調用時自己定位它。 – Cheery 2012-02-09 18:18:37

+0

@Cheery,完成更新:)再次感謝。 – 2012-02-09 18:22:58

2

另一個解決你的問題,我想,當我在一排被調用了大量的內置功能是我的圖書館暫時移動到路的盡頭。

libpath = '/home/user/mylib'; 
% move mylib to the end of the path 
addpath(libpath, '-end'); 
% now call some built-in functions that mylib overwrites 
reshape(rand(100),10,10); 
% return mylib to the top 
addpath(libpath) 

當然,如果你使用內置的功能往往比你的libary的,你可以保持在庫路徑的終點,它只要您使用其移動到頂部。請注意您的當前目錄,但是,在路徑順序上始終需要precedence

1

安德烈的回答是不理想的我,但它和羅蘭的建議,「到該目錄,創建 你的函數句柄,然後CD回來」讓我想到以下幾點:

定義一個函數,做什麼羅倫描述:

function functionHandle = getFunctionHandleFromFile(fullFileName) 

[pathstr, name, ext] = fileparts(fullFileName); 

prevDir = pwd; 

cd(pathstr); 
functionHandle = str2func(name); 
cd(prevDir); 

然後,你可以用它來獲得句柄。隨着手柄,你可以調用函數:

nameOf = getFunctionHandleFromFile('/Users/sage/matlab-utilities/nameOf.m') 
nameOf(output) 

注到較新的MATLAB用戶:我建議謹慎使用此方法!在某些情況下它可能非常有幫助,但總的來說,我會問自己,如果沒有更好的方法來處理您正在嘗試解決的問題。這可能會造成比解決更多的麻煩。

+0

問題是當函數想要從當前工作目錄讀取/寫入文件時,您的方法會中斷。 – 2013-09-09 17:33:26

+0

另請參閱相關問題http://stackoverflow.com/questions/13072470/call-a-function-that-is-not-on-the-matlab-path-without-adding-that-path/22532918#22532918和FEX貢獻:http://www.mathworks.com/matlabcentral/fileexchange/45941-constructor-for-functionhandles – 2016-04-27 08:20:30

相關問題