How to extract ony numerical data points from .dat files contained in a folder?
5 views (last 30 days)
Show older comments
I've various .dat files contained in a folder. All these .dat files contains some header lines, which needs to be skip while these .dat file containes data points of Peak-accleleration, Peak velocity & so. I want these data points in different text files, such that (one text file contain only peak acceleration data points only stored in a single column only while the second column of this text file will contain time-period having equal step size).
0 Comments
Answers (1)
Arun
on 18 Apr 2024
Hi Parvesh,
I understand that you have various ".dat" files and want to extract data from them to store in text files, where you wish to skip some header lines and have separate text file for different columns along with time-period values.
Following are the steps along with sample code snippets that can be used to get the required files:
Step-1 : The attached file is a .txt file, convert it to .mat file
% This step is required if you the the file to be in .mat format
data = readmatrix("UTTARKASHI EARTHQUAKE, OCT 20,1991.txt");
save('fileName.mat','data');
Step-2 : Set up the environment: Specify the folder where .dat files are located, number of header lines to skip, and the time step values.
folderPath = ''; % Change this to your folder path
headerLines = 5; % Change this according to your files
timeStep = 0.01; % Define your time step size for the second column
Step-3: Read and Process files:
a. List all .dat files in the folder.
files = dir(fullfile(folderPath, '*.dat'));
b. Loop through each file process and write the data to a new text file:
for k = 1:length(files)
% Construct the full file path
filePath = fullfile(files(k).folder, files(k).name);
% Read the data, skipping header lines
data = readmatrix(filePath, 'NumHeaderLines', headerLines);
% Assuming the first column is Peak Acceleration, adjust if necessary
peakAcceleration = data(:, 1);
% Generate the time-period column based on the number of data points and the step size
timePeriod = (0:timeStep:(length(peakAcceleration)-1)*timeStep)';
% Combine into a new matrix for writing to file
outputData = [peakAcceleration, timePeriod];
% Define the output file name and path (change 'output' to your desired output folder)
filename = files(k).name;
outputFilePath = fullfile('', strcat('', filename, '.txt'));
% Write the data to a new text file
writematrix(outputData, outputFilePath, 'Delimiter', 'tab');
end
This will help you get started, please modify the code according to your implementation to get the required files.
For more information about file operation, refer the share documentation link: https://www.mathworks.com/help/matlab/file-operations.html
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!