function ExpRead3

% Since it's not quite easy to plot the Thermo-Calc simulation results,
% this script is dedicated for the easy handle of TC results using matlab.
% The basic idea contains reading the EXP file, and then plot them. As I,
% myself is not a expert in TC, hence my understanding of EXP file is far
% from complete. I will keed updating it to include new file format I'll
% meet. Feel free to contact me by renqingqiang@gmail.com. 

[file,path] = uigetfile('*.exp','EXP file generated by Thermo-Calc');
old=path;
f=strcat(path,file);
mkdir(strcat(file,'-sub'));
cd (strcat(file,'-sub'));
copyfile(f,pwd)
fid = fopen(f,'r');
fid0 = fopen(strcat(file,'.dat'),'w');
tline = 'qingqiang'; % I do this just for fun.
while(~feof(fid))
    if (length(tline) >= 9 && strcmp(tline(1:9),'$ PLOTTED'))
        tmp = regexp(tline, '\s+', 'split');
        xaxis = char(tmp(length(tmp)-2));
        yaxis = char(tmp(length(tmp)));
        tline = fgetl(fid);
        tline = fgetl(fid);
        tline = fgetl(fid);
%         expression = '\'';
%         splitStr = regexp(str,expression,'split')
        tmp = strsplit(tline,'''');
        index = tmp(length(tmp));
        index=char(index);
        dlmwrite(strcat(file,'.intro'),strcat(xaxis, 32, yaxis, 32,index),'-append','delimiter', '')

        
%         if (exist(index,
        
        fprintf(fid0,'%s \t %s \t %s \n',index,xaxis,yaxis); 
        fprintf(fid0,'%s \n', ' ');
        dlmwrite(index,strcat(xaxis, 32, yaxis, 32,index),'-append','delimiter', '')
        tline = fgetl(fid);
        tmp = regexp(tline, '\s+', 'split');
        if (strcmp(tmp(length(tmp)),'M'))
            while (tline(1) ~= 'B' && ~strcmp(tline(1:9),'$ PLOTTED'))
                dlmwrite(index,tline,'-append','delimiter', '');
                fprintf(fid0,'%s \n',tline);
                tline = fgetl(fid);
            end
        else
            while (~strcmp(tmp(length(tmp)),'M'))
                tline = fgetl(fid);
                tmp = regexp(tline, '\s+', 'split');
            end
            while (tline(1) ~= 'B' && ~strcmp(tline(1:9),'$ PLOTTED'))
                fprintf(fid0,'%s \n',tline);
                dlmwrite(index,tline,'-append','delimiter', '');
                tline = fgetl(fid);
            end
        end
    else
        tline = fgetl(fid);
    end
end
fclose('all');
cd ..\
end
