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

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

  •  29-06-2022
  •  | 
  •  

문제

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)

도움이 되었습니까?

해결책 2

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

or if you want them as numbers then

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top