Conveter a string with space in a number

조회 수: 1 (최근 30일)
Hildo
Hildo 2015년 6월 20일
편집: Stephen23 2020년 8월 26일
A have one vector returned by the function textscan which have spaces inside the numerical string.
E.g.:
' 0 0 , 2 3 '
' 1 2 , 1 5 '
First, a have to substitute the ',' by '.' (it's simple) but how I remove the spaces? Because using str2num in this vector is returned error.
  댓글 수: 2
Stephen23
Stephen23 2020년 8월 26일
편집: Stephen23 2020년 8월 26일
"A have one vector returned by the function textscan which have spaces inside the numerical string."
I am surprised that no one brought up the obvious solution, to fix the (likely) problem at its source by handling the file encoding properly. Then textscan would import the data correctly.
Image Analyst
Image Analyst 2020년 8월 26일
Well, someone might have, but the original poster forgot to attach the original text file. That is a common omission. For some reason, many/most posters totally ignore the posting guidelines that they are shown when they post the question, one of which says to attach their data.

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

채택된 답변

Image Analyst
Image Analyst 2015년 6월 20일
It's pretty much the same except that you set it equal to [] instead of a decimal point:
str(str == ' ') = []; % Remove spaces
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 6월 20일
cellfun(@(str)str(str~=' '), dataArray, 'Uniform', 0)
Walter Roberson
Walter Roberson 2015년 6월 20일
cellfun(@(str)str(2:2:end), dataArray, 'Uniform', 0)
in the case where the odd-numbered characters are the blanks.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 20일
편집: Azzi Abdelmalek 2015년 6월 20일
s={' 0 0 , 2 3 ';'1 2 , 1 5 '}
a=strrep(s,',','.')
b=strrep(a,' ','')
out=str2double(b)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by