read text file - header and numerical part
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Akzeptierte Antwort
dpb
am 10 Okt. 2022
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")
tEnd= datetime(strtrim(extractAfter(hdr(ixTime(2)),'ing:')),'inputformat',"dd/MM/uuuu HH:mm:ss")
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))
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...
0 Kommentare
Weitere Antworten (1)
Benjamin Thompson
am 10 Okt. 2022
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!