Conveter a string with space in a number

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 Kommentare

Stephen23
Stephen23 am 26 Aug. 2020
Bearbeitet: Stephen23 am 26 Aug. 2020
"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 am 26 Aug. 2020
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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 20 Jun. 2015

0 Stimmen

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

3 Kommentare

Hildo
Hildo am 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.
cellfun(@(str)str(str~=' '), dataArray, 'Uniform', 0)
cellfun(@(str)str(2:2:end), dataArray, 'Uniform', 0)
in the case where the odd-numbered characters are the blanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 20 Jun. 2015
Bearbeitet: Azzi Abdelmalek am 20 Jun. 2015

0 Stimmen

s={' 0 0 , 2 3 ';'1 2 , 1 5 '}
a=strrep(s,',','.')
b=strrep(a,' ','')
out=str2double(b)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 20 Jun. 2015

Bearbeitet:

am 26 Aug. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by