str = "sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"},
I would like to extract the number 3637, 3638, and 2787 from the above string. How should I do it? Thanks!

댓글 수: 1

Something is wrong with that because it throws this error —
str = "sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}
Operator ':' is not supported for operands of type 'string'.

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

 채택된 답변

Voss
Voss 2022년 9월 12일

0 개 추천

str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
numbers = regexp(str,'"(\d+)"','tokens');
numbers = [numbers{:}]
numbers = 1×3 cell array
{'3637'} {'3638'} {'2787'}
numbers = str2double(numbers)
numbers = 1×3
3637 3638 2787

추가 답변 (1개)

Hiro Yoshino
Hiro Yoshino 2022년 9월 12일

1 개 추천

You can use pattern:
pat = digitsPattern(4); % Pattern
str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
numbers = extract(str,pat);
string(numbers)
ans = 3×1 string array
"3637" "3638" "2787"

댓글 수: 1

Jasmine Zhu
Jasmine Zhu 2022년 9월 13일
Thank you for your reply. Unfortunately my matlab doesn't support digitsPattern().

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2022년 9월 12일

댓글:

2022년 9월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by