學(xué)習(xí)的實(shí)驗(yàn)往往會(huì)記錄大量的原始數(shù)據(jù),excel處理起來很不方便,好在都是格式化的员辩,用matlab編寫了一個(gè)方法躯护,方便自己在不同場(chǎng)景套用。
讀寫格式化的數(shù)據(jù)文件(用數(shù)據(jù)類型table的方法)
% data analysis: 6 contexts;seperation data from different contexts
function contextSeperation(subName)
% subName = '6001_jushanzhong';
%curFile = fopen([subName,'.txt'],'r');
matData = readtable([subName,'.txt']);
% correct error in former recording
errData = matData.Error;
for i = 1 : length(errData)
if errData(i) < - 180
errData(i) = errData(i) + 360;
end
end
matData.correctedErr = errData;
% add new row 'rotation'
matData.bias = matData.RealTarget-matData.ShowTarget;
% add new row 'subjectName'
f = @(x) subName;
cellName = cell(length(errData),1);
cellName = cellfun(f,cellName,'UniformOutput',false);
matData.subName = cellName;
arrCenter = matData.CenterInd;
uniqCenter = unique(arrCenter);
numCenter = length(uniqCenter);
newTrialList = [1:10 1:40 1:10]';
% 按序號(hào)分出來
for k = 1 : numCenter
tmpInd = find(arrCenter == uniqCenter(k));
tmpArr = matData(tmpInd,:);
tmpArr.Trial = newTrialList;
writetable(tmpArr,['seperate\',subName,'_',num2str(k),'.txt'],'Delimiter','\t');
end
end
批處理文件夾中所有同類文件
% 讀取目錄下所有的文件信息
fileList = dir(cd);
listLength = length(fileList);
for i = 1 : listLength
if ~fileList(i).isdir
full_name = [cd,'\',fileList(i).name];
[pathstr,name,ext] = fileparts(full_name); % 獲取需要處理的文件文件名和拓展名
if strcmp(ext,'.txt')
contextSeperation(name);
end
end
end
合并文件夾中所有數(shù)據(jù)文件
之前數(shù)據(jù)量小的文件一直都用的批處理.bat來合并怒坯,最近數(shù)據(jù)量大了這個(gè)功能總是出問題,還是自己寫一個(gè)比較放心。
fileList = dir(cd);
listLength = length(fileList);
C = [];
for i = 1 : listLength
if ~fileList(i).isdir
full_name = [cd,'\',fileList(i).name];
[pathstr,name,ext] = fileparts(full_name); % 獲取需要處理的文件文件名和拓展名
if strcmp(ext,'.txt') % 要拼合的文件類型
tmpTable = readtable(full_name);
if isempty(C)
C = tmpTable;
else
C = [C; tmpTable];
end
end
end
end
writetable(C,'STS_all.txt','Delimiter','\t');
(新增)合并在一個(gè)文件的寫法
subDir = 'procedure';
fileList = dir([cd, '\',subDir]);
listLength = length(fileList);
for i = 1 : listLength
if ~fileList(i).isdir
full_name = [cd, '\',subDir,'\',fileList(i).name];
[pathstr,name,ext] = fileparts(full_name); % 獲取需要處理的文件文件名和拓展名
if strcmp(ext,'.txt')
funcReProTable(subDir,name);
end
end
end
function funcReProTable(subDir,name)
matData = readtable([subDir,'\',name,'.txt'],'Format','%s%s%u%s%s%u%u%f%f%s%s%u%f%f%s%s%s%s%s%s%s','Delimiter','\t');
splitname = strsplit(name,'_');
% remove useless column
matData = removevars(matData,{'DisplayCat','isCatchTrial','CurBlockCorrect','CurBlockCatchTrialCorrect','BlocktotAcc','BlockCatchTrialtotAcc','Var20','Var21'});
% add new row 'CatCond'
CatCond = cell(length(matData.SubName),1);
CatCond(:) = {splitname(2)};
matData = addvars(matData,CatCond,'Before','CurBlock');
% sep test from learning
testInd = [find(strcmp(matData.CurBlock,'oldStim')) find(strcmp(matData.CurBlock,'normStim'))];
testData = matData(testInd,:);
testData.CurBlock(:) = {'test'};
testData = removevars(testData,{'RT02'});
testData = renamevars(testData,"RT01","RT");
learnInd = 1 : testInd(1,1)-1;
learnData = matData(learnInd,:);
learnData = removevars(learnData,{'RT01'});
learnData = renamevars(learnData,"RT02","RT");
% procedureFile
proTable = [learnData; testData];
writetable(proTable,['data\procedure\limitPT_procedure_',proTable.CatCond{1}{1},'_',proTable.ExpCond{1},'_',proTable.SubName{1},'_old.txt'],'Delimiter','\t');
end