How to create an array of integers from a string
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi everyone, I have a string of the form: 'm:n, i, j' and I want to create an array with those integers. For example, if my string was '1:3, 10, 11' then the desired array would be [1, 2, 3, 10, 11]. I've tried using the eval function but couldn't figure out how to assign the result of eval to an array. Does anyone have any suggestions?
댓글 수: 0
채택된 답변
Azzi Abdelmalek
2014년 8월 12일
편집: Azzi Abdelmalek
2014년 8월 12일
s='1:3, 10, 11'
out=str2num(s)
With eval it should be something like
s='1:3, 10, 11'
eval(strcat('out=[', s ,']'))
댓글 수: 2
Azzi Abdelmalek
2014년 8월 12일
It's recommended to avoid eval function, read this http://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html#bs7kmd5
추가 답변 (1개)
Jan
2014년 8월 12일
Reduce the use of EVAL as far as possible:
s = '1:3, 10, 11'
out = eval(['[', s, ']']);
If the format is strictly 'm:n, i, j', this would be cleaner than eval:
d = sscanf(s, '%g:%g, %g, %g')
out = [d(1):d(2), d(3), d(4)]
댓글 수: 0
참고 항목
카테고리
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!