How do you delete text data from a row if its characters are equal to or less than 3

I have a 163x1 row of text data, the data that I would like to replace with "" has 3 characters, while the data I want to keep has greater than 3 characters. Below is a section of the data:

'Trinidad & Tobago'
  'USD'
  'Turkey'
  'TRY'
  'USD'
  'Uganda'
  'UGX'
  'Ukraine'
  'USD'
  'United Arab Emirates'
  'USD'
  'United Kingdom'
  'USD'
  'United States'
  'USD'
  'Uruguay'
  'UYU'
  'Zambia'
  'USD'
  'NA'
  'BRL'
  'COP'
  'CZK'
  'EUR'

1 Comment

How are these data currently stored? For example, do you have them in a cell array? Or in a CSV file, not yet loaded into MATLAB?

Sign in to comment.

 Accepted Answer

If you have the data in a cell array:
C = {'Trinidad & Tobago'
'USD'
'Turkey'
'TRY'
'USD'
'Uganda'
'UGX'
'Ukraine'
'USD'
'United Arab Emirates'
'USD'
'United Kingdom'
'USD'
'United States'
'USD'
'Uruguay'
'UYU'
'Zambia'
'USD'
'NA'
'BRL'
'COP'
'CZK'
'EUR'}
then
C = C(cellfun(@(x)length(x)>3,C))
will keep only the longer entries, while
C(cellfun(@(x)length(x)<=3,C)) = {''}
will replace the 3-character entries with empty string.

More Answers (1)

a = string({'Trinidad & Tobago'
'USD'
'Turkey'
'TRY'
'USD'
'Uganda'
'UGX'
'Ukraine'
'USD'
'United Arab Emirates'
'USD'
'United Kingdom'
'USD'
'United States'
'USD'
'Uruguay'
'UYU'
'Zambia'
'USD'
'NA'
'BRL'
'COP'
'CZK'
'EUR'});
a(strlength(a) <= 3)="";

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!