MATLAB: Very strange problem with text file reading (fscanf)

reading text file

Hi, I am trying to load dates and times from a file. For the first file everything works fine, but for the second file gives me matlab this error: ??? Error using ==> horzcat CAT arguments dimensions are not consistent.
The weird thing is, these files contains almost the same values. They are different in only one number. Can somebody please tell me, how to modify Matlab file to be working with both files? Thanks for answers.
content of loadDatesAndTimes.m
loadedFile=uigetfile('*.txt');
fid = fopen(loadedFile);
% skip first line in the file
fscanf(fid, '%c',32);
datetest = 0;
i = 0;
while ~feof(fid)
i=i+1;
year = fscanf(fid,'%i',4);
fscanf(fid,'%c',1);
month = fscanf(fid,'%i',2);
fscanf(fid,'%c',1);
day = fscanf(fid,'%i',1);
fscanf(fid,'%c',1);
hour = fscanf(fid,'%i',2);
fscanf(fid,'%c',1);
minute = fscanf(fid,'%i',1);
datetest(1,i) = datenum([num2str(year),' ',num2str(month),' ',num2str(day),' ',num2str(hour),' ',num2str(minute)], 'yyyy mm dd HH MM');
fscanf(fid,'%c',1);
if ( strcmp(fscanf(fid,'%c',4),'none') ) % compare strings
datetest(2,i) = 0; % device is OK
else
status = fseek(fid,-4,'cof'); % return 4 characters back and start reading
year = fscanf(fid,'%i',4);
fscanf(fid,'%c',1);
month = fscanf(fid,'%i',2);
fscanf(fid,'%c',1);
day = fscanf(fid,'%i',1);
fscanf(fid,'%c',1);
hour = fscanf(fid,'%i',2);
fscanf(fid,'%c',1);
minute = fscanf(fid,'%i',1);
datetest(2,i) = datenum([num2str(year),' ',num2str(month),' ',num2str(day),' ',num2str(hour),' ',num2str(minute)], 'yyyy mm dd HH MM');
end % end if
end % end while
fclose(fid);
format('long')
disp(datetest*24);
content of first text file
start time time of failure
2011.01.01 00:00 2011.01.01 06:00
2011.01.01 06:00 none
2011.01.01 00:00 none
2011.01.01 00:00 2011.01.01 03:00
2011.01.01 00:00 2011.01.01 07:00
content of second text file
start time time of failure
2011.01.01 00:00 2011.01.01 06:00
2011.01.01 06:00 none
2011.01.01 00:00 none
2011.01.01 00:00 2011.01.01 03:00
2011.01.01 00:00 2011.01.01 08:00

Best Answer

Use "dbstop if error" to let Matlab stop when the error occurs. Then inspect the current values of all variables to find the cause of the error.
This does most likely not, what you expect:
year = fscanf(fid,'%i',4)
It does not read an integer with 4 digits, but tries to read 4 integers without success. I suggest this to read the time:
data = fscanf(fid, '%d.%d.%d %d:%d', 5);
year = data(1);
month = data(2); ... etc