MATLAB: How to remove redundant commas when writing a txt-file from a table

csvmultiple commasredundant commastxt filewritetable

I created a txt-file from a table (139×39) 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

Best Answer

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);