Matlab - Removing extracting certain charecters/numbers from char cells in a cell array

StackOverflow https://stackoverflow.com/questions/18918351

  •  29-06-2022
  •  | 
  •  

Frage

I have a Matlab cell array, A <118080 x 1 cell> that look something like this:

"Point 1"
"Point 2"
"Point 3"
...
"Point 1230"

The cells are char cells of between 1x9 and 1x12 dimensions.

I need to seperate the numbers from these fields to give a 118080 x 1 matrix like:

1
2
3
...
1230

Any help would be greatly appreciated.

Best

Sam (A Matlab Newbie)

War es hilfreich?

Lösung 2

cellfun(@(x)(x(7:end)), A, 'UniformOutput', false)

or if you want them as numbers then

cellfun(@(x)(str2num(x(7:end))), A)

Andere Tipps

A solution without cellfun that uses regexp (only the numbers are kept here)

A = {'Point 1'
     'Point 222'
     'Point 33333'}

B = regexp(A, '\d+', 'match');  %produce a cell array of numbers in string format

If you want to convert the cells into a matrix

B = str2double([B{:}])';        %convert to numbers
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top