How to convert string into array?
조회 수: 10 (최근 30일)
이전 댓글 표시
Hello, I have a string named area in which string are stored as shown below:
1 2296.0 220.9 232.5
2 814.0 344.7 263.9
3 993.0 372.9 241.4
4 2406.0 469.7 228.5
I want to convert this string into array. So I used cellstr(area) but what I got is all these values in one cell only.
' 1 2296.0 220.9 232.5'
' 2 814.0 344.7 263.9'
' 3 993.0 372.9 241.4'
' 4 2406.0 469.7 228.5'
' 4 2406.0 469.7 228.5'
What I want is every value in different cell. Like, in first column 1,2,3,4 etc. In 2nd column 2296.0, 814.0 etc. How can I do this?
댓글 수: 0
채택된 답변
Walter Roberson
2017년 8월 14일
t = regexp(regexprep(cellstr(area), '^\s+', ''), '\s+', 'split');
CC = str2double(vertcat(t{:}));
댓글 수: 0
추가 답변 (2개)
Naseeb Gill
2017년 8월 13일
댓글 수: 5
Stephen23
2017년 8월 14일
편집: Stephen23
2017년 8월 14일
@NASEEB SINGH: str2num hides eval inside, and eval is a bit rsky to use as it can execute arbitrary code (also is slow, hard to debug, etc): https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!