Read every nth line of text file with different deliminters across a single line
3 ビュー (過去 30 日間)
古いコメントを表示
Have a large at least one million line test files that are formatted as below with forward slash as part of date, colons with time, and tabs between the data:
currently using to following code to read in the files where impliment fgets to skip over N# lines. However, that does not seem to speed up the process all that much. I was wondering if that was faster way to read in this type of fille which has a few different deliminters across the line.
numRows = 0; %Set counter for each row read
Time = zeros(1e6, 1); %Preallocate
PTCM = zeros(1e6, 4); %Preallocate
SkipLines=input('Enter # of lines to skip: ');
fid = fopen('laserBPTC.txt', 'rt'); % open to read as text
while 1 %Loop through each line of file
dt = fscanf(fid, '%d/%d/%d %d:%d:%d ', 6); %Read in Date/Time
if isempty(dt), break, end %EOF so break out of read loop
newTime = datenum(dt(1), dt(2), dt(3), dt(4), dt(5), dt(6)); %Contert to Matlab date/time
data = fscanf(fid, '%lf', 4); %Read in 4 data items
numRows = numRows + 1; %New row;
Time(numRows, 1) = newTime; %Store time
PTCM(numRows, 1:4) = data(1:4); %Store data
for skipLine=0:SkipLines %Skip lines
[~]=fgets(fid);
end
end
Time = Time(1:numRows); %Tuncate to off preallocated lines
PTCM = PTCM(1:numRows,:); %Tuncate to off preallocated lines
fclose(fid); %Close file
2019/01/09 16:31:44 783.9 21.84 2.088 4.835
2019/01/09 16:31:48 782.5 21.87 2.084 4.835
2019/01/09 16:31:52 782.5 21.71 2.084 4.835
2019/01/09 16:31:56 778.5 21.73 2.092 4.835
2019/01/09 16:32:00 779.8 21.82 2.088 4.835
2019/01/09 16:32:04 777.1 21.66 2.088 4.835
0 件のコメント
採用された回答
Bob Thompson
2019 年 4 月 3 日
You might have an easier time using textscan instead of fgets or dlmread. You have to specify your format for a row, but that should allow you to end up just using the white space as a delimiter. Below is an example formatting input for your sample given,
data = textscan('mytextfile.txt','%(yyyy/mm/dd hh:mm:ss)D %f %f %f %f');
3 件のコメント
その他の回答 (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!