clear; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Paramenters Section % Group folders contain sample subfolers in which subclass-specific XIC is named as consecutive numbers as a form of text file dirName = '.\Late stages'; % Set the start point of each subclass dividePoint = [1,22,31]; % Set the export path statFile = '.\stat_result_.txt'; % Annotation note = {'IgG 1', 'IgG 4', 'IgG 2'}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if dirName(end) ~= '\' dirName = [dirName, '\']; end grpNum = length(dividePoint); if length(note) ~= length(dividePoint) fprintf('The length of note should be the same as dividePoint.\n'); return end dirList = dir(dirName); pattern = '^(\D+)(\d+)'; % Fold selection '^(\D+)(\d+)' tokens_ = regexp({dirList.name}, pattern, 'tokens') ; % Keep the part of letter uniform strPart = cell(1,length(tokens_)); for d = 2:length(tokens_) if ~isempty(tokens_{1,d}) stdStrPart = tokens_{1,d}{1,1}{1,1}; break; end end flag = 0; for dcnt = (d+1):length(tokens_) if stdStrPart ~= tokens_{1,dcnt}{1,1}{1,1} fprintf('The folder name in %s is not the same as the %s.\n', dirList(dcnt).name, tokens_{1,dcnt}{1,1}{1,1}); fprintf('The string part of subfolders'' name should be the same.\n'); flag = 1; end end if flag == 1 flag = 0; return; end % Sorted by number numPart = -1.23*ones(1,length(tokens_)); numStrPart = cell(1,length(tokens_)); for dcnt = 1:length(tokens_) if ~isempty(tokens_{1,dcnt}) numStrPart{dcnt} = tokens_{1,dcnt}{1,1}{1,2}; numPart(dcnt) = str2double(tokens_{1,dcnt}{1,1}{1,2}); end end numStrPart(numPart==-1.23)=[]; numPart(numPart==-1.23)=[]; % This two lines can't be reverse [dirNameNum, dirNameIdx] = sort(numPart, 2,'ascend'); numStrPart = numStrPart(dirNameIdx); % Number of valid folder dirNum = length(dirNameNum); % The file name and amount of first subfold is set as the standard for variable initialization dcnt = 1; stdFolderName = [stdStrPart, numStrPart{dcnt}]; subDirName = [dirName, stdFolderName, '\']; stdFileList = dir([subDirName, '*.txt']); stdFileNum = length(stdFileList); % Collecting the amout of text files in the first subfold flag = 0; stdFileNameList = cell(1, stdFileNum); for i = 1:stdFileNum stdFileNameList{1, i} = [num2str(i), '.txt']; end stdFileNameList = sort(stdFileNameList); % File names must be consecutive numbers if ~isequal(stdFileNameList, {stdFileList.name}) fprintf('The file name in %s is not consist of continuous numbers.\n', stdFolderName); flag = 1; return end fprintf('Prepare to use the file names in %s as the standard names.\n', stdFolderName); for dcnt = 2:dirNum iterDirName = [stdStrPart, numStrPart{dcnt}]; subDirName = [dirName, iterDirName, '\']; fileList = dir([subDirName, '*.txt']); fileNum = length(fileList); if fileNum ~= stdFileNum fprintf('The file number in %s is not the same as the %s.\n', iterDirName, stdFolderName); flag = 1; continue; end if ~isequal({fileList.name}, {stdFileList.name}) fprintf('The file names in %s is not the same as the %s.\n', iterDirName, stdFolderName); flag = 1; continue; end end if flag == 1 flag = 0; return; end % Building the format reading the string of txt files readFormat = '%f'; % Five columns in a txt file is the default colsNum = 5; rowsNum = 1; for cnt = 1:colsNum-1 readFormat = strcat(readFormat, '\t%f'); end % sumStats = zeros(dirNum, stdFileNum); % Data storage % Start reading data dividePoint = [dividePoint,stdFileNum+1]; headSet = dividePoint(1:end-1); footSet = dividePoint(2:end); memSpace = footSet - headSet; summary = cell(grpNum, dirNum); records = cell(grpNum, dirNum); for dcnt = 1:dirNum subDirName = [dirName, stdStrPart, numStrPart{dcnt}, '\']; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for gcnt = 1:numel(dividePoint)-1 % Obtaining the break point for each subclass timeList = zeros(memSpace(gcnt),1); areaList = zeros(memSpace(gcnt),1); for mcnt = 1:memSpace(gcnt) fname = sprintf('%d.txt',dividePoint(gcnt)+mcnt-1); fullName = fullfile(subDirName,fname); fid = fopen(fullName, 'r'); if fid == -1 fsprintf('Cannot open the file %s.\n',fullName); return; end inMat = textscan(fid,readFormat,'headerlines',1); % Read the file time = inMat{1,1}; area = inMat{1,2}; fclose(fid); if size(time ,1)~=rowsNum fprintf('The data format of time in %s is incorrect.\n',fullName); return; end if size(area ,1)~=rowsNum fprintf('The data format of area in %s is incorrect.\n',fullName); return; end timeList(mcnt) = time; areaList(mcnt) = area; end areaSum = sum(areaList); proportion = areaList / areaSum * 100; records{gcnt, dcnt} = [timeList, areaList, proportion]; summary{gcnt, dcnt}= proportion; end end % Data exported in a standard form basicDist = '\t\t\t\t'; dcnt = 1; header = [stdStrPart, numStrPart{dcnt}]; exNote = note; for i = 1:length(note) exNote{i} = [note{i}, basicDist]; end % Processing record part dataFormat = '%f\t%f\t%f\t\t'; for dcnt = 2:dirNum subDirName = [stdStrPart, numStrPart{dcnt}]; header = strcat(header, basicDist, subDirName); for i = 1:length(note) exNote{i} = [exNote{i}, note{i}, basicDist]; end dataFormat = strcat(dataFormat, '%f\t%f\t%f\t\t'); end % SummaryÁÐ header = [header, basicDist, 'Summary']; for dcnt = 1:dirNum subDirName = [stdStrPart, numStrPart{dcnt}]; header = strcat(header, '\t', subDirName); dataFormat = strcat(dataFormat, '\t%f'); end header = [header, '\n']; dataFormat = [dataFormat, '\n']; for i = 1:length(note) exNote{i} = [exNote{i}, '\t', note{i}, '\t\t\n']; end % Writing the data fID = fopen(statFile,'w'); if fID == -1 fprintf('Cannot open the file %s.\n',statFile); return; end fprintf(fID,header); for gcnt = 1:grpNum fprintf(fID,exNote{gcnt}); grpRecordData = cell2mat(records(gcnt, :)); grpSummaryData = cell2mat(summary(gcnt, :)); matData = [grpRecordData, grpSummaryData]; for h = 1:size(matData, 1) fprintf(fID, dataFormat, matData(h, :)); end end fclose(fID); fclose all;