readmatrix ignores the first line of the data file

조회 수: 104 (최근 30일)
Jake
Jake 2023년 3월 9일
편집: Stephen23 2023년 3월 9일
I'm trying to read a text filw with over 226,600 rows with readmatrix (sample is over 5MB so I couldn't attach it here). The first line is numeric but readtable ignores the first line. Following is the line I'm using is follows, and I do require the structure (with NaN) that this provides.
data = readmatrix('sample.txt', 'Delimiter',' ','ConsecutiveDelimitersRule', 'join');
I found a similar thread where table2array was suggested, but I get the "Input argument must be a table." error. How can I rectify this?
** edit: A trimmed sample file is uploaded.
  댓글 수: 2
Stephen23
Stephen23 2023년 3월 9일
"How can I rectify this?"
Did you try specifying the first data line, or the number of header lines, or any other related options?
Please trim the file down a bit and upload it here.
Jake
Jake 2023년 3월 9일
Hi @Stephen23, I uploaded a trimmed data file.
The solution provided by @Sarvesh Kale seems to work for the time being, but I'd rather have a method where I don't have to add a line manually, since soon I will be dealing with multiple files similar to this.

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

채택된 답변

Stephen23
Stephen23 2023년 3월 9일
편집: Stephen23 2023년 3월 9일
You can specify the RANGE as the starting row:
M = readmatrix('sample.txt', 'ConsecutiveDelimitersRule','join', 'Range',1)
M = 11020×15
-1.0000 1.0000 1.0000 0.0303 0 -0.0446 0 0.1337 0 -0.0016 0 0.0647 0 0.0461 0 0.0027 0 0.0012 0 0 0 0 0 -0.0027 0 -0.0012 0 NaN NaN NaN -0.0054 0 -0.0025 0 -0.0082 0 -0.0037 0 -0.0109 0 -0.0049 0 NaN NaN NaN -0.0136 0 -0.0061 0 -0.0164 0 -0.0072 0 -0.0191 0 -0.0083 0 NaN NaN NaN -0.0219 0 -0.0094 0 -0.0246 0 -0.0104 0 -0.0274 0 -0.0114 0 NaN NaN NaN -1.0000 1.0000 2.0000 0.0318 0 -0.0505 0 0.1375 0 -0.0017 0 0.0679 0 0.0521 0 0.0030 0 0.0015 0 0 0 0 0 -0.0030 0 -0.0015 0 NaN NaN NaN -0.0061 0 -0.0030 0 -0.0092 0 -0.0045 0 -0.0122 0 -0.0060 0 NaN NaN NaN -0.0153 0 -0.0075 0 -0.0184 0 -0.0089 0 -0.0215 0 -0.0103 0 NaN NaN NaN -0.0246 0 -0.0116 0 -0.0277 0 -0.0129 0 -0.0308 0 -0.0141 0 NaN NaN NaN
I note that the data are arranged in blocks which span multiple lines. Note that if all files have a fixed, known format then you might consider using FSCANF to reads those blocks of data, giving one row per block, e.g.:
nmc = 15+12*4;
fmt = repmat('%f',1,nmc);
fid = fopen('sample.txt','rt');
mat = fscanf(fid,fmt,[nmc,Inf]).';
fclose(fid);
display(mat)
mat = 2204×63
-1.0000 1.0000 1.0000 0.0303 0 -0.0446 0 0.1337 0 -0.0016 0 0.0647 0 0.0461 0 0.0027 0 0.0012 0 0 0 0 0 -0.0027 0 -0.0012 0 -0.0054 0 -0.0025 -1.0000 1.0000 2.0000 0.0318 0 -0.0505 0 0.1375 0 -0.0017 0 0.0679 0 0.0521 0 0.0030 0 0.0015 0 0 0 0 0 -0.0030 0 -0.0015 0 -0.0061 0 -0.0030 -1.0000 1.0000 3.0000 0.0305 0 -0.0402 0 0.1327 0 -0.0013 0 0.0649 0 0.0418 0 0.0024 0 0.0012 0 0 0 0 0 -0.0024 0 -0.0012 0 -0.0048 0 -0.0024 -1.0000 1.0000 4.0000 0.0301 0 -0.0314 0 0.1318 0 -0.0010 0 0.0634 0 0.0327 0 0.0019 0 0.0009 0 0 0 0 0 -0.0019 0 -0.0009 0 -0.0038 0 -0.0018 -1.0000 1.0000 5.0000 0.0308 0 -0.0316 0 0.1329 0 -0.0009 0 0.0646 0 0.0330 0 0.0019 0 0.0010 0 0 0 0 0 -0.0019 0 -0.0010 0 -0.0037 0 -0.0020 -1.0000 1.0000 6.0000 0.0316 0 -0.0416 0 0.1357 0 -0.0012 0 0.0672 0 0.0433 0 0.0025 0 0.0014 0 0 0 0 0 -0.0025 0 -0.0014 0 -0.0050 0 -0.0028 -1.0000 1.0000 7.0000 0.0326 0 -0.0533 0 0.1392 0 -0.0017 0 0.0700 0 0.0550 0 0.0032 0 0.0017 0 0 0 0 0 -0.0032 0 -0.0017 0 -0.0064 0 -0.0035 -1.0000 1.0000 8.0000 0.0324 0 -0.0444 0 0.1379 0 -0.0012 0 0.0692 0 0.0461 0 0.0026 0 0.0016 0 0 0 0 0 -0.0026 0 -0.0016 0 -0.0053 0 -0.0031 -1.0000 1.0000 9.0000 0.0333 0 -0.0559 0 0.1413 0 -0.0016 0 0.0722 0 0.0575 0 0.0033 0 0.0020 0 0 0 0 0 -0.0033 0 -0.0020 0 -0.0067 0 -0.0039 -1.0000 1.0000 10.0000 0.0349 0 -0.0650 0 0.1443 0 -0.0021 0 0.0741 0 0.0663 0 0.0039 0 0.0022 0 0 0 0 0 -0.0039 0 -0.0022 0 -0.0077 0 -0.0044

추가 답변 (1개)

Sarvesh Kale
Sarvesh Kale 2023년 3월 9일
Hi Jake,
You can open the text file in notepad and insert some text that best describes your data on the first line, from second line your data will start and I guess this should resolve your problem
Thank you
  댓글 수: 1
Jake
Jake 2023년 3월 9일
Hi, @Sarvesh Kale, this seems to work but it would be quite difficult to do this manually, since I will have to deal with several files soon. Thank you!

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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by