필터 지우기
필터 지우기

Parsing a text file by character

조회 수: 2 (최근 30일)
Ammar
Ammar 2014년 4월 8일
댓글: Alberto 2014년 4월 10일
I would like to parse a 331 string of characters into cells but the number of characters per cell is not the same for all?
For example, if I have text like the following
24359_435934009____________90909
where _=space
And I want to put it into a matrix such that
characters 1-5 go into column 1 (a.k.a 24359)
character 6 goes into column 2 (ak.a. NaN)
characters 7-10 goes into column 3
etc
The data comes as a .txt file.

답변 (2개)

Alberto
Alberto 2014년 4월 8일
If there are several lines, you can extract them using this:
fid=fopen(fileName)
B = textscan(fid, '%f %f %f', 'Delimiter'-' ', 'MultipleDelimsAsOne', 1);
fclose(fid)
It extracts three columns, if you need more columns change the part '%f %f %f'. But this only works if there are 3 numbres in each row.
If you have diferent numbers in each row, you should parse using regexp for each line.

Ammar
Ammar 2014년 4월 9일
편집: Ammar 2014년 4월 9일
That did not seem to work. I have attached example data. Each line should be 331 characters which includes spaces.
I want to parse the data into a matrix.
Column 1 should be characters 1-8 (so cell 1,1 should be 79006444)
Column 2 should be characters 9-18
Column 3 should be character 19
Column 4 should be characters 20-21
Column 5 should be characters 22
Column 6 should be character 23
Column 7 should be character 24
Column 8 should be characters 25-27
.....
areas with spaces need to be replaced with NaN
  댓글 수: 1
Alberto
Alberto 2014년 4월 10일
The extraction mode depends on what you know about the structure of the file. I think this should work:
B = textscan(fid, '%f %f %f %f %f %f %f %f', 'Delimiter', ' ', 'MultipleDelimsAsOne', 1);

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by