MATLAB: How to extract time and date data from huge text file

#dataextractionfgetlfscanftextscan

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

Best Answer

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