【研究工具】Matlab批量數(shù)據(jù)處理

學習的實驗往往會記錄大量的原始數(shù)據(jù),excel處理起來很不方便,好在都是格式化的,用matlab編寫了一個方法,方便自己在不同場景套用。

讀寫格式化的數(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]';

    % 按序號分出來
    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ù)量大了這個功能總是出問題,還是自己寫一個比較放心。

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');

(新增)合并在一個文件的寫法


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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容