相对于第一版的计算进行了改进
第一版的思路是把每一个txt中的每一行提取出来,对每一行进行判断,存储进入对应的cell,最后进行公式计算;
第二版的思路是把每一个txt中的每一个条件下的数据全部提取出来,然后将汇总后的条件数据进行mean计算;
clear all; clc; fileFolder=fullfile('D:\data\ZHJresults_133'); dirOutput=dir(fullfile(fileFolder)); fileNames={dirOutput.name}; [~,AN]=size(fileNames); res=[]; % feature('DefaultCharacterSet', 'UTF8'); Data=importdata([fileFolder,'\',fileNames{1,3}]); var=native2unicode(Data.textdata{2,1}(5)); for i = 3:AN %% basic prepration F=[]; W=[]; N=[]; Fitems=[]; Witems=[]; Nitems=[]; feature('DefaultCharacterSet', 'UTF8'); Data1=importdata([fileFolder,'\',fileNames{1,i}]); if isempty(Data1) F = 0; W=0; N=0; Fitems=0; Witems=0; Nitems=0; else [qq,pp]=size(Data1.textdata); %% total game time char=Data1.textdata{qq,1}; if length(char) ==13 TotalTime=str2double(char(7:11)); % min else if length(char) < 13 TotalTime=str2double(char(7:10)); else disp(['The total time of ',fileNames{1,i}, ' is unnormal.']); end end %% total game rounds GameRounds = qq-1; %% selection: F/W/N indexF=1; indexW=1; indexN=1; for n=1:qq-1 % game round msg = Data1.textdata{n,1}; items = Data1.data(n,:); % inter and items as a maxtre if ~isempty(strfind (msg,'F')) box = strsplit (msg,var); F(indexF,1)= str2double(box{1,3}(1:5)); if isempty(Fitems) Fitems = items; else Fitems = [Fitems;items]; end indexF = indexF+1; else if ~isempty(strfind (msg,'W')) box = strsplit (msg,var); W(indexW,1)= str2double(box{1,3}(1:5)); if isempty(Witems) Witems = items; else Witems = [Witems;items]; end indexW = indexW + 1; else box = strsplit (msg,var); N(indexN,1)= str2double(box{1,3}(1:5)); if isempty(Nitems) Nitems = items; else Nitems = [Nitems;items]; end indexN = indexN+1; end end end %% % types round Fround = size(F,1); Wround = size(W,1); Nround = size(N,1); % average inter time and item scores if ~isempty(F) F_averInter = mean(F); Fitem1_average = mean(Fitems(:,1)); Fitem2_average = mean(Fitems(:,2)); Fitem3_average = mean(Fitems(:,3)); else F_averInter = 0; Fitem1_average = 0; Fitem2_average = 0; Fitem3_average = 0; end if ~isempty(W) W_averInter = mean(W); Witem1_average = mean(Witems(:,1)); Witem2_average = mean(Witems(:,2)); Witem3_average = mean(Witems(:,3)); else W_averInter = 0; Witem1_average = 0; Witem2_average = 0; Witem3_average =0; end if ~isempty(N) N_averInter = mean(N); Nitem1_average = mean(Nitems(:,1)); Nitem2_average = mean(Nitems(:,2)); Nitem3_average = mean(Nitems(:,3)); else N_averInter = 0; Nitem1_average = 0; Nitem2_average = 0; Nitem3_average =0; end res(i-2,1) = str2double(fileNames{1,i}(length(fileNames{1,i})-3:length(fileNames{1,i}))); res(i-2,2)= TotalTime; res(i-2,3)= GameRounds; res(i-2,4)= F_averInter; res(i-2,5)= Fround; res(i-2,6)=Fitem1_average; res(i-2,7)=Fitem2_average; res(i-2,8)=Fitem3_average; res(i-2,9)=W_averInter; res(i-2,10)=Wround; res(i-2,11)=Witem1_average; res(i-2,12)=Witem2_average; res(i-2,13)=Witem3_average; res(i-2,14)=N_averInter; res(i-2,15)=Nround; res(i-2,16)= Nitem1_average; res(i-2,17)= Nitem2_average; res(i-2,18)= Nitem3_average; end end Result=array2table(res,'VariableNames',{'ID','TotalTime','T_round','F_inter','F_round','F_item1','F_item2','F_item3','W_inter','W_round','W_item1','W_item2','W_item3','N_inter','N_round','N_item1','N_item2','N_item3'}); writetable(Result,'D:\data\n133.xlsx'); msgbox('DONE!');
标签:01,end,res,average,Fitems,else,PLUS,DIY,mean From: https://www.cnblogs.com/MJnotes/p/16803657.html