필터 지우기
필터 지우기

convert a string vector to a vector of numbers

조회 수: 5 (최근 30일)
googo
googo 2013년 4월 16일
Hello,
suppose I have a vector of string [1 2 3 4] and i want to change it to a vector of nmbers to do a numeric caculations, How can I do it? Thank's!
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 4월 16일
Are the '[' and ']' in the string? Are the numbers only integers or possibly floating point ?
googo
googo 2013년 4월 16일
string=['1' '2' '3' '4'] and I want to change it to : a = [1 2 3 4]

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

답변 (1개)

Friedrich
Friedrich 2013년 4월 16일
편집: Friedrich 2013년 4월 16일
Hi,
as long there are no [] in the string use textscan:
textscan('1 2 3 4 6.3','%f','delimiter',' ')
In the case there are use strrep to remove the [ or ] before calling textscan.
  댓글 수: 8
Walter Roberson
Walter Roberson 2013년 4월 16일
You have
t(i,n+1)=num2str(c);
the destination t(i,n+1) is a single character. The number 10 and upwards require two (or more) characters to represent as a string. Therefore if c reached 10 (occurrences) the code would fail. This suggests that you have a code design problem.
googo
googo 2013년 4월 16일
Thank you... I'll try to find a way to fix it..

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

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by