Removing first row from .txt files and convert them to .csv format in one folder

4 次查看(过去 30 天)
Hello,
I would like to to convert all txt file in a folder to .csv format without header. I went through various solutions on this forum and they worked only partially or not at all.
I would like to ask you for help.
File 1:
X Location (m) Y Location (m) Z Location (m) Temperature (°C)
0.218899995 0.197255 -3.89899989e-003 22.
0.21783933 0.197255 -4.33835154e-003 22.
0.217399999 0.197255 -5.39901108e-003 22.
0.217839345 0.197255 -6.45966409e-003 22.
File 2:
X Location (m) Y Location (m) Z Location (m) Temperature (°C)
0.218899995 0.197255 -3.89899989e-003 22.
0.21783933 0.197255 -4.33835154e-003 22.
0.217399999 0.197255 -5.39901108e-003 22.
0.217839345 0.197255 -6.45966409e-003 22.
Best regards
Michal

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-12-14
Here is a general way of reading data from multiple files and performing operations on it.
I have changed the temperature values of the 2nd file to show as an example.
%Get all the files in the folder ending with .txt format
%Update the search text if there is a unique string related to all files
files = dir('*.txt')
files = 2×1 struct array with fields:
name folder date bytes isdir datenum
%Run a for loop through all the files
for k=1:numel(files)
%Get the full path to the file
fullname = fullfile(files(k).folder, files(k).name);
%Read the data via readmatrix()
%As the output will only contain numeric values, not the text header
in = readmatrix(fullname);
%Write the data as a csv file, by changing the extension of the file
writematrix(in, replace(fullname, '.txt', '.csv'));
end
%Checking the output
type('File1.csv')
0.218899995,0.197255,-0.00389899989,22 0.21783933,0.197255,-0.00433835154,22 0.217399999,0.197255,-0.00539901108,22 0.217839345,0.197255,-0.00645966409,22
type('File2.csv')
0.218899995,0.197255,-0.00389899989,23 0.21783933,0.197255,-0.00433835154,24 0.217399999,0.197255,-0.00539901108,25 0.217839345,0.197255,-0.00645966409,26

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by