How to extract time and date data from huge text file?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(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!