Script Converting txt to CSV
이전 댓글 표시
Trying to create a scrip that converts txt to csv.
I need to skip the first line.
All rows thereafter (variable), have a timestamp in format DD-MM-YYYY HH:MM:SS AM/PM followed by five more columns of data with character delimaters W,C,A,B,R.
Any ideas how to get this running?
Codestart:

Sample data lines
06-14-2019 12:56:24 PM
06-14-2019 12:56:37 PM W-00.3C2.20e-12A6.32B3.34R029
06-14-2019 12:56:42 PM W-00.4C5.15e-13A6.31B3.32R062
06-14-2019 12:56:47 PM W-00.7C4.06e-13A6.29B3.31R118
답변 (2개)
ag
2025년 2월 6일
Hi Patrick,
To convert the text file to csv, you can use the "fgetl" MATLAB function to read and process each line.
Below is a self explanatory code snippet, that demonstrates how you can do that:
% Define the format for the data lines
formatSpec = '%s %f %f %f %f %f';
% Read and process each line from the input file
% Ignore the first line
fgetl(fileIdTxt);
while ~feof(fileIdTxt)
% Read the next line
line = fgetl(fileIdTxt);
% Split the line using the delimiters
parts = split(line, {' ', 'W', 'C', 'A', 'B', 'R'});
% Check if the line was split into the expected number of parts
if length(parts) == 7
% Extract the timestamp and data
timestamp = parts{1};
data = str2double(parts(2:end));
% Write the data to the output CSV file
fprintf(fileIdCsv, '%s,%.2f,%.2f,%.2f,%.2f,%.2f\n', timestamp, data);
end
end
For more details, please refer to the following MathWorks documentations:
- fgetl - https://www.mathworks.com/help/matlab/ref/fgetl.html
- feof - https://www.mathworks.com/help/matlab/ref/feof.html
- split - https://www.mathworks.com/help/matlab/ref/string.split.html
Hope this helps!
댓글 수: 2
Including ' ' (space) in the set of delimiters gives 9 parts with this example data, not 7, with the timestamp split into three parts
line = '06-14-2019 12:56:37 PM W-00.3C2.20e-12A6.32B3.34R029';
parts = split(line, {' ', 'W', 'C', 'A', 'B', 'R'})
Better to exclude the space because then you have 6 nicely delimited parts, with the timestamp all together.
parts = split(line, {'W', 'C', 'A', 'B', 'R'})
Also, your formatSpec variable is unused.
And when you write to csv, you can use %s format for all parts, since that's consistent with they already are (no need to convert to double unless you need that validation step).
fileIdTxt = fopen('test.txt','r');
fileIdCsv = fopen('test.csv','w');
% Read and process each line from the input file
% Ignore the first line
fgetl(fileIdTxt);
while ~feof(fileIdTxt)
% Read the next line
line = fgetl(fileIdTxt);
% Split the line using the delimiters
parts = split(line, {'W', 'C', 'A', 'B', 'R'});
% Write the data to the output CSV file
fprintf(fileIdCsv, '%s,', parts{:});
fprintf(fileIdCsv, '\n');
end
type test.csv
ag
2025년 2월 6일
Hey Voss, thanks for pointing out the mistakes and providing the suggestions.
str = readlines('test.txt')
str(1) = [];
C = regexp(str,'[WCABR]','split');
out = vertcat(C{:});
writematrix(out,'test.csv')
% check the csv file's contents
type test.csv
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!