Very strange problem with text file reading (fscanf)
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!