2012-03-15 254 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。