This is the sample of the input file (first few lines) and the whole for loop:
fid = fopen('test.txt');
PT = textscan(fid, '%s %s %f %f','Delimiter',',', 'HeaderLines',1);
fclose(fid);
clear ans fid
% Organize data (remove 00:00:00) and convert date to Matlab format
PT{1} = cellfun(@(x) strrep(x, ' 00:00:00',''), PT{1},'UniformOutput',false);
PerRow = cell2mat(strfind(PT{:,1},'.'));
for i=1:length(PT{:,1})
% add two zeros if format is m.d.yyyy
if PerRow(i,1) == 2 && PerRow(i,2) == 4
PT{1}{i,1} = ['0', PT{1,1}{i,1}(1:2), '0', PT{1}{i,1}(3:end)];
% Add one zero if format is d.mm.yyyy
elseif PerRow(i,1) == 2 && PerRow(i,2) ~=4
PT{1}{i,1} = ['0', PT{1}{i,1}];
% Add one zero if format is dd.m.yyyy
elseif PerRow(i,2) == 5
PT{1}{i,1} = [PT{1}{i,1}(1:3), '0', PT{1}{i,1}(4:end)];
end
% Add zero if format is H
if size(PT{1,2}{i},2) == 1
PT{1,2}{i}=['0', PT{1,2}{i}];
end
% Combain columns and transform date/time to Matlab format
PT{1}{i,1} = datenum(strcat(PT{1,1}{i,1},PT{1,2}{i,1}),'dd.mm.yyyyHH');
end
clear i PerRow
The loop without the problematic line takes only about 20 sec on my Linux computer.