Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove element from array matlab

Tags:

matlab

i have a array that contains all the files in a particular directory. I want to remove all the file entries that end with the .txt extension. This is what i have written

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 a 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

like image 294
klijo Avatar asked Mar 15 '12 07:03

klijo


People also ask

How do you remove an element from a cell array in MATLAB?

Delete the contents of a particular cell by assigning an empty array to the cell, using curly braces for content indexing, {} . Delete sets of cells using standard array indexing with smooth parentheses, () .

How do you remove NaN values from an array in MATLAB?

Method 1: By using rmmissing( ) This function is used to remove missing entries or Nan values from a specified matrix.

How do you delete a string in MATLAB?

newStr = erase( str , match ) deletes all occurrences of match in str . The erase function returns the remaining text as newStr . If match is an array, then erase deletes every occurrence of every element of match in str . The str and match arguments do not need to be the same size.

How do you do logical indexing in MATLAB?

In logical indexing, you use a single, logical array for the matrix subscript. MATLAB extracts the matrix elements corresponding to the nonzero values of the logical array. The output is always in the form of a column vector. For example, A(A > 12) extracts all the elements of A that are greater than 12.


1 Answers

You can avoid the function and for-loop with the single line construction

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

The isempty() construction returns true if the filename does not include '.txt'. The oldList(...) construction returns a cell array of elements of oldList for which the isempty construction returns true.

like image 113
lsfinn Avatar answered Oct 07 '22 18:10

lsfinn