Clear Filters
Clear Filters

How do I remove redundant commas when writing a txt-file from a table?

10 views (last 30 days)
I created a txt-file from a table (139x39) which contains some blanks. The data is separated by commas as delimiter. Unfortunately in the resulting file, commas also separate the blanks:
Raw Data,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
"Friday, Dec 05, 2019,17:00:28",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
191205.txt,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
95,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
16,16,16,1,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
CPS,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Time,7Li,31P,39K,43Ca,44Ca,45Sc,47Ti,55Mn,77ArCl,82Se,83Kr,85Rb,88Sr,89Y,90Zr,93Nb,133Cs,137Ba,139La,140Ce,141Pr,146Nd,147Sm,153Eu,159Tb,160Gd,163Dy,165Ho,166Er,169Tm,172Yb,175Lu,178Hf,181Ta,182W,208Pb,232Th,238U
0,0,1300,1900,0,200,0,0,400,0,750,700,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
%underneath this are another 100 rows with data in the same format as the previous row
Ideally, the content of the file should look like this - any idea how to do this?
Also if someone knows a trick on how to remove the double-quotes in line 2, that would be much appreciated.
Raw Data
Friday, Dec 05, 2019,17:00:28
191205.txt
95
0
16,16,16,1,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
CPS
Time,7Li,31P,39K,43Ca,44Ca,45Sc,47Ti,55Mn,77ArCl,82Se,83Kr,85Rb,88Sr,89Y,90Zr,93Nb,133Cs,137Ba,139La,140Ce,141Pr,146Nd,147Sm,153Eu,159Tb,160Gd,163Dy,165Ho,166Er,169Tm,172Yb,175Lu,178Hf,181Ta,182W,208Pb,232Th,238U
0,0,1300,1900,0,200,0,0,400,0,750,700,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Accepted Answer

Image Analyst
Image Analyst on 28 Jan 2020
You could read in the file and use strrep() to replace each ,, with , until there are no ,, left. Basically (untested)
fid = fopen(filename, 'rt');
fidOut = fopen(outputFileName, 'wt')
textLine = fgetl(fid);
while ischar(textLine)
while contains(textLine, ',,')
textLine = strrep(textLine, ',,', ',');
end
% Now write out
fprintf(fidOut, '%s\n', textLine);
% Now read in the next line.
textLine = fgetl(fid);
end
fclose(fid);
fclose(fidOut);
  3 Comments
Jule
Jule on 28 Jan 2020
Ah, there was an accidental additional space in 'rt ' - it works with 'rt'.
This works except for the last comma in the row, thanks!!
Image Analyst
Image Analyst on 28 Jan 2020
You can get rid of trailing comma like this
if endsWith(textLine, ',')
% textLine ends with a comma. Strip it off.
textLine = textLine(1:end-1);
end
You might look at Stephen's code. It's more compact, though more cryptic if you don't know how to construct regular expressions.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 28 Jan 2020
Edited: Stephen23 on 28 Jan 2020
Regular expressions make this easy (removes all trailing commas, removes the double quotes, and leaves the newlines unchanged):
str = fileread('test.txt')
str = regexprep(str,{',+(\s*\n)','"([^\n"]+)"'},'$1')
[fid,msg] = fopen('test_new.txt','w');
assert(fid>=3,msg)
fprintf(fid,'%s',str)
fclose(fid);
Giving:
Raw Data
Friday, Dec 05, 2019,17:00:28
191205.txt
95
0
16,16,16,1,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
CPS
Time,7Li,31P,39K,43Ca,44Ca,45Sc,47Ti,55Mn,77ArCl,82Se,83Kr,85Rb,88Sr,89Y,90Zr,93Nb,133Cs,137Ba,139La,140Ce,141Pr,146Nd,147Sm,153Eu,159Tb,160Gd,163Dy,165Ho,166Er,1
69Tm,172Yb,175Lu,178Hf,181Ta,182W,208Pb,232Th,238U
0,0,1300,1900,0,200,0,0,400,0,750,700,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Community Treasure Hunt

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

Start Hunting!