How to get the number in the cell containing the letter and space

조회 수: 3 (최근 30일)
eko supriyadi
eko supriyadi 2022년 6월 1일
댓글: dpb 2022년 6월 2일
Hi everyone
consider i have cell matrix like this:
a={'AABB 01001';'AACC 01101'; 'AACC 01201'; 'AADD 01301'; 'AAEE 01401'}
so, it will produce:
a =
5×1 cell array
{'AABB 01001' }
{'AACC 01101' }
{'AACC 01201' }
{'AADD 01301' }
{'AAEE 01401'}
Now, I have no idea how to take the numbers only (because contain letter and single-multiple space).. so i want the result is (with leading zeros in a number too):
a = 01001
01101
01201
01301
01401
the a result is double format.
thank you for your help :)
  댓글 수: 4
dpb
dpb 2022년 6월 1일
>> compose('%05d',str2double(extractAfter(a,' ')))
ans =
5×1 cell array
{'01001'}
{'01101'}
{'01201'}
{'01301'}
{'01401'}
>>
Stephen23
Stephen23 2022년 6월 2일
"with leading zeros in a number too"
No numeric class on any common computer platform stores leading zeros, so what you ask for is not possible.
You could store the data as text, in which case you can keep the leading zeros.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 6월 2일
편집: Stephen23 2022년 6월 2일
No indirect, fragile, round-about conversion to other data classes is required. Here are two direct approaches:
a = {'AABB 01001';'AACC 01101'; 'AACC 01201'; 'AADD 01301'; 'AAEE 01401'}
a = 5×1 cell array
{'AABB 01001' } {'AACC 01101' } {'AACC 01201' } {'AADD 01301' } {'AAEE 01401'}
b = extract(a,digitsPattern)
b = 5×1 cell array
{'01001'} {'01101'} {'01201'} {'01301'} {'01401'}
b = regexp(a,'\d+','match','once')
b = 5×1 cell array
{'01001'} {'01101'} {'01201'} {'01301'} {'01401'}

추가 답변 (1개)

dpb
dpb 2022년 6월 1일
편집: dpb 2022년 6월 1일
>> str2double(extractAfter(a,' '))
ans =
1001
1101
1201
1301
1401
>>
ADDENDUM:
>> compose('%05d',str2double(extractAfter(a,' ')))
ans =
5×1 cell array
{'01001'}
{'01101'}
{'01201'}
{'01301'}
{'01401'}
>>
or
>> string(compose('%05d',str2double(extractAfter(a,' '))))
ans =
5×1 string array
"01001"
"01101"
"01201"
"01301"
"01401"
>>
  댓글 수: 2
eko supriyadi
eko supriyadi 2022년 6월 1일
편집: eko supriyadi 2022년 6월 1일
b=str2double(extractAfter(a,' '))
for i=1:length(b)
zeroleading(i,:)=sprintf('%05d',b(i))
end
if i want keep zero, is the loop above correct?
dpb
dpb 2022년 6월 2일
If you don't need the numeric values as numeric, there's no point in converting them to numeric just to return them back to string or cellstr() to have the leading zero to display. In that case just extract the numeric pattern in one form or another -- I used the simplest syntax possible, the pattern or regular expression offers more generality.
If want a char() array instead, then just convert directly --
>> char(strtrim(extractAfter(a,' ')))
ans =
5×5 char array
'01001'
'01101'
'01201'
'01301'
'01401'
>>

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by