MATLAB中删除数组元素

6

我有一个包含特定目录中所有文件的数组。我想要删除所有以 .txt 扩展名结尾的文件项。这是我写的代码:

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 )

有人可以帮我吗?

这里的内容与IT技术无关。
2个回答

3

你可以使用一行代码来避免使用函数和for循环

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

isempty() 函数会在文件名中 不包含 '.txt' 时返回 true。oldList(...) 函数会返回一个由 oldList 中满足 isempty() 函数返回 true 的元素组成的单元数组。


2
在这种情况下,>0 是错误的。请改用~isempty(strfind(....))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接