Borrar filtros
Borrar filtros

Grabbing numbers from a string

4 visualizaciones (últimos 30 días)
matlabuser12
matlabuser12 el 23 de Jun. de 2015
Comentada: matlabuser12 el 23 de Jun. de 2015
I have a data set where some numbers are numbers like (124,456, 987) and others are numbers with an letter on them like (123P,232P) so when i read in [Data,Text] = xlsread(...) half my data set goes to data and the other half to text. The data variable puts NaN where the 124P type data was, how can I extract the number off the P and put it where the NaNs are?
  2 comentarios
per isakson
per isakson el 23 de Jun. de 2015
Editada: per isakson el 23 de Jun. de 2015
As a start, replace
[Data,Text] = xlsread(...)
by
[~,~,raw] = xlsread(___)
that makes it easier to keep track of the "cell" positions.
Then upload a sample of raw.
If the data is in a text file, it might be a good idea to bypass Excel.
matlabuser12
matlabuser12 el 23 de Jun. de 2015
Editada: matlabuser12 el 23 de Jun. de 2015
It is an .xls file unfortunately. This is a snippet from the data column:
115
118
118
121
118
'126P'
132

Iniciar sesión para comentar.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 23 de Jun. de 2015
Editada: Azzi Abdelmalek el 23 de Jun. de 2015
s={115;118;118;'121';118;'126P';132}
idx=cellfun(@isstr,s)
b=s(idx)
c=regexp(b,'\d+(\.\d+)?','match')
d=cellfun(@str2double,c,'un',0)
s(idx)=d
Or
s={115;118;118;'121';118;'126P';132}
out=cellfun(@(x) str2double(regexp(num2str(x),'\d+(\.\d+)?','match')),s)
  5 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 23 de Jun. de 2015
If you use the first code,add
out=str2double(out)
matlabuser12
matlabuser12 el 23 de Jun. de 2015
Got it to work with cel2mat following removal of the letter character. thank you

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by