Posts in category version

Filtering ESGF datasets versions in the metalink

clear all,fclose all;close all;clc;
nameFile='./hadgem2_es.metalink';
xmlLines=textread(nameFile,'%s','delimiter','\r\n');
endFiles=find(ismember(deblank(xmlLines),{'</file>'}));Nfiles=length(endFiles);
startFiles=[];fileProperties=cell(Nfiles,3);contador=1;
for i=1:length(xmlLines)
    sd=strfind(xmlLines{i},'<file name="');
    if ~isempty(sd)
        startFiles=[startFiles;i];
        fileName=['./' xmlLines{i}(sd+12:end-2)];
        [pathName,fileName,extFile]=fileparts(fileName);
        [aux1,aux2,vFile]=fileparts(pathName);vFile=vFile(2:end);
fileProperties{contador,1}=fileName;fileProperties{contador,2}=vFile;fileProperties{contador,3}=i;
        contador=contador+1;
    end
end
[fileNames,I1,I2]=unique(fileProperties(:,1));
lastFile=cell(length(I1),3);
for i=1:length(I1)
    indFiles=find(I2==i);
versiones=strvcat(fileProperties(indFiles,2));versiones=datenum([str2num(versiones(:,2:5)) str2num(versiones(:,6:7)) str2num(versiones(:,8:9)) repmat([0 0 0],length(indFiles),1)]);
[a1,a2]=max(versiones);lastFile(i,:)=fileProperties(indFiles(a2),:);
end
fid=fopen([nameFile '_last_version.metalink'],'w');
for i=1:min(startFiles)-1
    fprintf(fid,'%s \r\n',xmlLines{i});
end
Nfiles=length(I1);
for i=1:Nfiles
    indFile=find(startFiles==lastFile{i,3});
    for j=startFiles(indFile):endFiles(indFile)
        fprintf(fid,'%s \r\n',xmlLines{j});
    end
end
for i=max(endFiles)+1:length(xmlLines)
    fprintf(fid,'%s \r\n',xmlLines{i});
end
fclose(fid);