split special merged text and number in text file
Show older comments
Hi I've special text file (without any spaces or , between text and numbers) (attached file) and I want to split text and number in each lines for example: I have 'X004986Y002446' and I want to convert it to X=004986 and Y=002446 as two variable(Vector/Matrix) how to input text file and split each lines?
thanks
Accepted Answer
More Answers (1)
KSSV
on 21 Apr 2017
fid = fopen('data.txt') ;
S = textscan(fid,'%s','delimiter','\n') ;
fclose(fid) ;
S = S{1} ;
S = S(7:end-4) ;
out=regexp(S,'[\d]+','match') ;
out = [out{:}] ;
out = reshape(out',2,[])' ;
out=cell2mat(cellfun(@(x) str2double(x),out,'un',0)) ;
There would be more elegant way..
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!