read text file - header and numerical part

조회 수: 18 (최근 30일)
Iacopo
Iacopo 2022년 10월 10일
답변: dpb 2022년 10월 10일
Hi all,
I read a text file and separated the header from the matrix based on the attached example.
I’ve got 2 questions:
QUESTION 1 - I am able to read the header line by line and store them into a string array within a while cycle that stops at the last string (the header length can vary but the last line will always be the same). Or by using the function readlines.
Is there any way to convert a string array in char? Not char array but char only. Keeping the interruption line at the end of every line, the same result I would get with the function fileread.
I tried this:
header = readlines(‘Path of the text file’);
CHARheader = “”;
for i = 1:length(header)
CHARheader = strcat(CHARheader, header(i));
end
But the interruption characted is lost. I would like to keep it as I guess that would help me with the research:
Start_Time = extractBetween(CHARheader, ‘Start Recording:’, char(10))
Is that possible? And keeping the interruption characted, would help me with the research as written above?
An alternative, is there any way to stop the function readlines?
Or
A function similar to fileread that stops at any point decided by the user?
QUESTION 2 – To read the numerical part I am using radmatrix in the following way:
data = readmatrix(FilePath, 'NumHeaderLines', headerlines)
Where headerlines is the index of the while cycle I used to get the header line by line.
How is it possible to constrain the number of columns and rows to extract? Using the option Range gives me an error.
Thanks for your kind support!

채택된 답변

dpb
dpb 2022년 10월 10일
Q1: A. See fgets to keep newline
B: Don't need ExtractBetween, use ExtractAfter; then don't need the actual \n in the data
C: Don't worry about stopping; just delete after the last line of interest although is very long the fgetl route is still a possibility not to discount.
hdr=readlines(websave('TEST_readingTextFile_MATLAB.txt','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1151100/TEST_readingTextFile_MATLAB.txt'));
hdr=hdr(strlength(hdr)>0);
idxData=find(matches(hdr,"AMPLITUDE EQUALIZED"))+3;
data=hdr(idxData:end);
hdr=hdr(1:idxData-1);
ixTime=find(contains(hdr,'recording:'));
tStart= datetime(strtrim(extractAfter(hdr(ixTime(1)),'ing:')),'inputformat',"dd/MM/uuuu HH:mm:ss")
tStart = datetime
15-Jul-2022 09:31:13
tEnd= datetime(strtrim(extractAfter(hdr(ixTime(2)),'ing:')),'inputformat',"dd/MM/uuuu HH:mm:ss")
tEnd = datetime
15-Jul-2022 12:31:13
Q2: Again, read the whole thing and then just keep what don't want/need instead of worrying about skipping on reading -- will be just as fast (or maybe even faster as file sizes grows). I'd also recommend to look at timetable as another possible data storage mechanism depending upon what is to be done with data in end.
data=str2double(split(data))
data = 11×7
0 -0.0005 -0.0015 0.0006 -0.0055 -0.0015 -0.0058 0.0010 -0.0010 -0.0022 -0.0011 -0.0054 -0.0011 -0.0031 0.0020 -0.0008 -0.0019 -0.0019 -0.0061 -0.0011 -0.0000 0.0030 0.0004 0.0001 -0.0003 -0.0052 -0.0013 0.0026 0.0040 0.0001 0.0011 0.0021 -0.0054 -0.0010 0.0039 0.0050 -0.0014 -0.0003 0.0016 -0.0047 -0.0008 0.0048 0.0060 -0.0024 -0.0027 -0.0000 -0.0051 -0.0010 0.0042 0.0070 -0.0004 -0.0014 0.0016 -0.0046 0.0000 0.0027 0.0080 0.0015 0.0019 0.0039 -0.0056 -0.0005 0.0003 0.0090 0.0012 0.0025 0.0035 -0.0059 -0.0004 -0.0014
If the above turns out to be slow with full-length files, post back and we'll attack speeding up -- it would basically be a mesh of what you've done and the above...

추가 답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 10월 10일
Try detectImportOptions and readtable, setting the VariableNamesLine of the options structure to the line that you are looking for in the text file. You could also try the data import wizard, set it up there, and then generate the sample code to read the file from the import wizard.

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by