MATLAB Answers

Conveter a string with space in a number

28 views (last 30 days)
Hildo
Hildo on 20 Jun 2015
Edited: Azzi Abdelmalek on 20 Jun 2015
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.

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 20 Jun 2015
It's pretty much the same except that you set it equal to [] instead of a decimal point:
str(str == ' ') = []; % Remove spaces

  3 Comments

Hildo
Hildo on 20 Jun 2015
How I can use this in this data annex (it's a cell data)? I try cellfun(@(str)str(str~=' '),dataArray{1,2}) but not work.
Walter Roberson
Walter Roberson on 20 Jun 2015
cellfun(@(str)str(str~=' '), dataArray, 'Uniform', 0)
Walter Roberson
Walter Roberson on 20 Jun 2015
cellfun(@(str)str(2:2:end), dataArray, 'Uniform', 0)
in the case where the odd-numbered characters are the blanks.

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 20 Jun 2015
Edited: Azzi Abdelmalek on 20 Jun 2015
s={' 0 0 , 2 3 ';'1 2 , 1 5 '}
a=strrep(s,',','.')
b=strrep(a,' ','')
out=str2double(b)

  0 Comments

Sign in to comment.

Sign in to answer this question.