Search code examples
matlabwhitespacecell

white space in cell array


I am using textscan to read text file and I get <55x1 cell> examples:

 'aa a aa'
 'a  aaaa a'
 'a =  aaaaa'
 'aaaaaa'
 ' a a a aaa'
 'aa'
 'aaa'
 'aaaa'
  .
  . 
  .
  .

I want to delete the white spaces in each sting If I have a sting

 string = 'I am 24 Years    old'

And I use

  string(ismember(string,' ')) = [];

it will eliminate the spaces and I will get

 'Iam24Yearsold'

But with the cell doesn't work or I don't know how to do it How can I do that? any suggestions please?


Solution

  • You can use strrep:

    a = { 'aa a aa'
     'a  aaaa a'
     'a =  aaaaa'
     'aaaaaa'
     ' a a a aaa'
     'aa'
     'aaa'
     'aaaa'
     'I am 24 Years    old'};
    
    strrep(a, ' ', '')
    

    This results in

    ans = 
    
        'aaaaa'
        'aaaaaa'
        'a=aaaaa'
        'aaaaaa'
        'aaaaaa'
        'aa'
        'aaa'
        'aaaa'
        'Iam24Yearsold'