read text file - header and numerical part
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
dpb
2022-10-10
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 个评论
更多回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!