如何在MATLAB中批量导入并处理Log_*.txt数据文件
Log_*.txt Files in MATLAB Got it, let's turn your single-file processing script into a fully functional batch workflow. Here's a step-by-step solution that will automatically process all your Log_*.txt files and combine them into a single unified table:
Step 1: Get the list of all target files
First, use MATLAB's dir function to locate every Log_*.txt file in your working directory (specify a full path if your files are stored elsewhere):
% Get all Log_*.txt files in the current directory logFiles = dir('Log_*.txt'); % If files are in a different folder, use the full path instead: % logFiles = dir('C:\Your\Data\Storage\Folder\Log_*.txt');
Step 2: Initialize a master table for all processed data
We'll start with an empty table that matches the structure of your processed single-file data to ensure consistency:
% Define the final variable names once (matches your existing setup) finalVarNames = {'essai', 'DureeEssai', 'azVoulue', 'elVoulu', 'azMesure','elMesure', 'distanceAngulaire', 'diffhorizontale', 'diffElevation', 'x1', 'y1', 'z1', 'x2', 'y2', 'z2', 'posTx', 'posTy', 'postz', 'oriTx', 'oriTy', 'oriTz', 'oriTw', 'posCx', 'posCy', 'posCz', 'oriCx', 'oriCy', 'oriCz','oriCw','distanceAngulaire_oriT_C','az_oriT','el_oriT'}; % Create empty master table with the correct schema masterT = table('VariableNames', finalVarNames);
Step 3: Loop through each file and run your processing logic
Now we'll iterate over every file, execute your existing processing steps, and append the results to the master table:
% Define delimiters once (no need to redefine in the loop) delim = {')(',') ','(', ' ', ','}; for i = 1:length(logFiles) % Get the full path to the current file filePath = fullfile(logFiles(i).folder, logFiles(i).name); % Run your existing processing code T = readtable(filePath,'Delimiter', delim,'MultipleDelimsAsOne',1); T = removevars(T, {'Var1', 'Var2', 'Var4', 'Var6', 'Var8', 'Var10', 'Var12','Var14', 'Var16', 'Var18', 'Var26', 'Var27', 'Var31', 'Var36', 'Var40', 'Var45', 'Var47', 'Var49'}); T.Properties.VariableNames = finalVarNames; T = T(2:2:end,:); % Optional: Add a column to track which source file each row came from T.sourceFile = repmat(logFiles(i).name, height(T), 1); % Append the processed table to the master table masterT = [masterT; T]; end
Step 4: Save or utilize your unified dataset
Once the loop finishes, masterT will contain all your processed data from every Log_*.txt file. You can save it for later analysis:
% Save as a CSV file (easy to open in Excel or other tools) writetable(masterT, 'all_processed_subject_data.csv'); % Or save as a MATLAB .mat file for future use in MATLAB save('all_processed_subject_data.mat', 'masterT');
Quick tips for robustness:
- Recursive search: If your
Log_*.txtfiles are spread across subfolders, usedir('**/Log_*.txt');(works in MATLAB R2016b and later) to find them all. - Error handling: If some files might have inconsistent structures, add a
try/catchblock inside the loop to skip problematic files and log errors. - Performance: For very large datasets, preallocating the master table can speed up processing, but the above code works perfectly for most research workflows.
内容的提问来源于stack exchange,提问作者Nocturne

