2017-07-17 148 views
4

我正在遷移我的代碼,並且需要儘可能減少已用工具箱的數量。例如,我有一個使用多個工具箱的大型腳本文件。我能找到這些使用從特定的matlab工具箱中查找使用的函數

[fList,pList] = matlab.codetools.requiredFilesAndProducts('myscript.m'); 
display({pList.Name}'); 

我得到以下結果

'Image Processing Toolbox' 
'Instrument Control Toolbox' 
'MATLAB' 
'Model-Based Calibration Toolbox' 
'Signal Processing Toolbox' 
'Statistics and Machine Learning Toolbox' 
'Parallel Computing Toolbox' 

有一個簡單的辦法知道哪些功能是從特定的工具箱在我的腳本文件中使用?例如,我如何知道在我的代碼中使用'Model-Based Calibration Toolbox'的哪個函數?或者使用工具箱的哪一行代碼?這樣我就可以嘗試自己實現這個功能並避免使用工具箱。

注意:我需要在所有本地函數和嵌套函數中包含工具箱依賴項,以及這些函數(完全依賴項樹)中使用的函數。例如一個gui文件有許多本地回調函數。

+1

嘗試運行[依賴性報告(https://uk.mathworks.com/help/matlab/matlab_prog/identify-依賴關係.html),這會告訴你哪個函數會導致一個特定的工具箱被使用。應該有一個超鏈接指向調用這些函數的代碼的特定行。 – am304

回答

4

你可以使用的半記錄功能 getcallinfo一個文件調用的函數的名稱:

g = getcallinfo('filename.m'); 
f = g(1).calls.fcnCalls.names; 

在一般情況下,該文件可能有子功能,並且g是一種非標結構陣列。 g(1)引用文件中的主函數,而f是一個單元格數組,其中包含所調用函數的名稱。 f有每個呼叫的條目(這些呼叫發生的線路是g(1).calls.fcnCalls.lines)。然後,您可以找到使用which這些功能:

cellfun(@(x) which(x), unique(f)) 

其中unique來刪除重複的函數名。但請注意,which看到的功能可能與您的文件看到的功能不同,具體取決於搜索路徑。


舉個例子,內置的文件perms.m給出:

>> g = getcallinfo('perms.m') 

>> g(1) 
ans = 
    struct with fields: 

       type: [1×1 internal.matlab.codetools.reports.matlabType.Function] 
       name: 'perms' 
      fullname: 'perms' 
    functionPrefix: 'perms>' 
      calls: [1×1 struct] 
     firstline: 1 
      lastline: 37 
      linemask: [61×1 logical] 

>> g(2) 
ans = 
    struct with fields: 

       type: 'subfunction' 
       name: 'permsr' 
      fullname: 'perms>permsr' 
    functionPrefix: 'perms>permsr' 
      calls: [1×1 struct] 
     firstline: 40 
      lastline: 61 
      linemask: [61×1 logical] 

>> f = g(1).calls.fcnCalls.names 
f = 
    1×8 cell array 
    'cast' 'computer' 'error' 'factorial' 'isequal' 'length' 'message' 'numel' 

>> cellfun(@(x) which(x), unique(f)) 
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\datatypes\cast) 
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\general\computer) 
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\lang\error) 
C:\Program Files\MATLAB\R2016b\toolbox\matlab\specfun\factorial.m 
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elmat\isequal) 
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elmat\length) 
message is a built-in method % message constructor 
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elmat\numel) 
+0

謝謝路易斯,這太好了。我假設如果我通過'g'循環,我可以獲得所有本地功能(子功能)。但是,這是否會將更深層次的呼叫包含在層次結構中?如果沒有,是否只有這樣才能實現這個目標?逐一完成每個依賴關係並做同樣的事情? –

+0

@VaheTshitoyan不,它只包括第一關。在我的例子中,'factorial'例如調用'cumprod',這裏不包括。您需要手動執行遞歸,但我不確定這是否實用 –

+2

感謝您的澄清,這開始看起來像文件交換項目 –