Home > Net >  Removing Measurement Units from Cell Array
Removing Measurement Units from Cell Array

Time:06-15

I am trying to remove the units out of a column of cell array data i.e.:

cArray =
time                 temp
2022-05-10 20:19:43  '167 °F'
2022-05-10 20:19:53  '173 °F'
2022-05-10 20:20:03  '177 °F'
...
2022-06-09 20:18:10  '161 °F'

I have tried str2double but get all NaN.

I have found some info on regexp but don't follow exactly as the example is not the same.

Can anyone help me get the temp column to only read the value i.e.:

cArray =
time                 temp
2022-05-10 20:19:43  167
2022-05-10 20:19:53  173
2022-05-10 20:20:03  177
...
2022-06-09 20:18:10  161

CodePudding user response:

For some cell array of data

cArray = { ...
  1,   '123 °F'
  2,   '234 °F'
  3,   '345 °F'
  };

The easiest option is if we can safely assume the temperature data always starts with numeric values, and you want all of the numeric values. Then we can use regex to match only numbers

temps = regexp( cArray(:,2), '\d ', 'match', 'once' );

The match option causes regexp to return the matching string rather than the index of the match, and once means "stop at the first match" so that we ignore everything after the first non-numeric character.

The pattern '\d ' means "one or more numbers". You could expand it to match numbers with a decimal part using '\d (\.\d )?' instead if that's a requirement.

Then if you want to actually output numbers, you should use str2double. You could do this in a loop, or use cellfun which is a compact way of achieving the same thing.

temps = cellfun( @str2double, temps, 'uni', 0 ); % 'uni'=0 to retain cell array

Finally you can override the column in cArray

cArray(:,2) = temps;
  • Related