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

per isakson
per isakson 2015년 6월 23일
편집: per isakson 2015년 6월 23일
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 2015년 6월 23일
편집: matlabuser12 2015년 6월 23일
It is an .xls file unfortunately. This is a snippet from the data column:
115
118
118
121
118
'126P'
132

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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 23일
편집: Azzi Abdelmalek 2015년 6월 23일

0 개 추천

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

matlabuser12
matlabuser12 2015년 6월 23일
This seems to work, but how do i convert teh cells to numbers?
Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 23일
편집: Azzi Abdelmalek 2015년 6월 23일
with the second method, they are already double
s={115;118;118;'121';118;'126P';132}
out=cellfun(@(x) str2double(regexp(num2str(x),'\d+(\.\d+)?','match')),s)
matlabuser12
matlabuser12 2015년 6월 23일
I used your first set of code and got rid of the S's. so I have an array s full of cells of numbers that i cannot convert into a regular matrix.
If you use the first code,add
out=str2double(out)
matlabuser12
matlabuser12 2015년 6월 23일
Got it to work with cel2mat following removal of the letter character. thank you

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2015년 6월 23일

댓글:

2015년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by