How to make this string a = '(0 0 0)' into a double b = [0 0 0]?

조회 수: 1 (최근 30일)
Wictor Oliveira
Wictor Oliveira 2022년 8월 5일
댓글: Walter Roberson 2022년 8월 5일
Suppose I don't know what are the number inside the a string, it could be:
a = '(12 2.8 1.22)' % each number is separated by a single space
which should then be:
b = [12 2.8 1.22]

채택된 답변

Kevin Holly
Kevin Holly 2022년 8월 5일
a = '(12 2.8 1.22)'
a = '(12 2.8 1.22)'
a = strrep(a,')',']');
a = strrep(a,'(','[');
b = str2num(a)
b = 1×3
12.0000 2.8000 1.2200

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2022년 8월 5일
편집: Fangjun Jiang 2022년 8월 5일
a = '(12 2.8 1.22)';
b=sscanf(a,'(%f %f %f)')
b = 3×1
12.0000 2.8000 1.2200
b=transpose(sscanf(a,'(%f %f %f)'))
b = 1×3
12.0000 2.8000 1.2200

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by