2012-03-15 252 views
5

我有一個數組,其中包含特定目錄中的所有文件。我想刪除所有以.txt擴展名結尾的文件條目。這就是我寫從陣列matlab中刪除元素

function fileList = removeElements(fileArray)  

    for idx = 1:numel(fileArray) 

    if (strfind(fileArray(idx),'.txt') > 0) 
    display('XX'); 
    fileArray(idx) =[]; 
    end  

    end  

end 

,但我得到一個錯誤

??? Undefined function or method 'gt' for input arguments of type 'cell'. 
    Error in ==> removeElements at 6 
     if(strfind(fileArray(idx),'.bmp') > 0) 

有人可以幫我

回答

1

>0是錯誤在這種情況下。改爲使用~isempty(strfind(....))

2

可以避開功能和循環與單線建設

% strip-out all '.txt' filenames 
newList = oldList(cellfun(@(c)(isempty(strfind('.txt',c))),oldList)); 

如果文件名中包括「.TXT」的的isEmpty()建築返回true。 oldList(...)構造函數返回一個oldList元素的單元數組,它的isempty構造返回true。