필터 지우기
필터 지우기

How to extract time and date data from huge text file?

조회 수: 2 (최근 30일)
Nikhil
Nikhil 2016년 1월 29일
답변: John BG 2016년 1월 29일
Dear all,
I am working on one big data science project. I need to extract data from one text file into separate columns of Matlab variable. I have attached smaller portion of the data along with this message. Can anybody please tell me where am I going wrong in my code for extracting data from text file? Thank you in advance, My MATLAB code is as follows:
fid = fopen('Nikhil.txt','r');
iread=0; icount = 0;
while(iread~=1)
icount=icount+1;
tline=fgetl(fid);
corr1=strfind(tline,'Date;Time;Global_active_power;Global_reactive_power;Voltage;Global_intensity;Sub_metering_1;Sub_metering_2;Sub_metering_3');
if(corr1~=0)
sizeA= [9 Inf];
[strdat, count]=fscanf(fid,['%D %D %g %g %g %g %g %g %g'],sizeA);
iread=iread+1;
end;
end;
%strdat=strdat';
fclose(fid);
n=length(strdat);
strdat

채택된 답변

John BG
John BG 2016년 1월 29일
your data file has a format that allows to import text without while fgetl strfind and fscanf
filename='Nikhil.txt'
delimiter = ';';
startRow = 2;
endRow = 50;
formatSpec2='%s %s %s %s %s %s %s %s %s';
fileID = fopen('DataSample.txt','r');
dataArray = textscan(fileID, formatSpec2, endRow-startRow+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'HeaderLines', startRow-1, 'ReturnOnError', false);
the columns are available in for instance
dataArray{1}
dataArray{2}
and to access single elements for instance
dataArray{1}{1}
dataArray{5}{3}
does this answer help? if so click on the thumb-up icon on the top of this page. Thanks
John

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by