MATLAB: Grabbing numbers from a string

nanstringstringstextscan

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?

Best Answer

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)