Remove item from matlab array

I have an array containing all the files in a specific directory. I want to delete all file entries that end with the .txt extension. This is what I wrote

function fileList = removeElements(fileArray)    

    for idx = 1:numel(fileArray)

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

    end      

end

but i get an error

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

Can someone please help me

+5
source share
2 answers

>0in this case not so. Use instead ~isempty(strfind(....)).

+1
source

You can avoid the function for a loop with the construction of a single line

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

The isempty () construct returns true if the file name does not contain '.txt'. The oldList (...) construct returns an array of cells of the oldList elements for which the empty construct returns true.

+2
source

All Articles