delete consecutive commas in txt file
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
george korris
am 12 Sep. 2022
Kommentiert: george korris
am 13 Sep. 2022
I have the next txt file that has more than one ',' between numbers and i only want 1 comma between my numbers
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
0.00443747926701899,0.0415007916135113,0.0507606123682882,,0.118547629187242,,,,,,,,,0.300291185258514,,,,,,,,,,,,,,,,,,,,0.410219670837715,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0.698099828162265
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2 Kommentare
Stephen23
am 12 Sep. 2022
What should happen with lines that only contain commas: do you want to keep them, or remove them?
Akzeptierte Antwort
Star Strider
am 12 Sep. 2022
Bearbeitet: Star Strider
am 12 Sep. 2022
Use readmatrix with additional name-value pair arguments to select the delimiter and combine multiple consecutive delimiters. See the documentation section on Text Files Only for details on these and other Name-Value Arguments.
M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1122645/ca.txt', 'Delimiter',',', 'ConsecutiveDelimitersRule','join', 'LeadingDelimitersRule','ignore')
.
4 Kommentare
Weitere Antworten (2)
Simon Chan
am 12 Sep. 2022
May be this:
rawdata = readlines('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1122645/ca.txt');
iwant = [];
for k = 1:length(rawdata)
data = str2double(strsplit(rawdata{k},','));
if ~all(isnan(data))
iwant = [iwant;data];
end
end
iwant
Mathieu NOE
am 12 Sep. 2022
hello
maybe not the best code but seems to do the job !
filename = 'ca.txt';
a = readlines(filename);
[m,~] = size(a);
count = 0;
for ci = 1:m
current_line = char(a(ci,:));
out_comas= strfind(current_line,',');
if numel(out_comas) < numel(current_line) % keep this line
count = count+1;
% remove extra comma separators
out_comas= strfind(current_line,',');
d = [0 diff(out_comas)];
ind = find(d == 1);
line_out = current_line;
line_out(out_comas(ind)) = [];% remove extra comma separators
C{count,1} = line_out;
end
end
% export to txt file
writecell(C,'ca_out.txt',"QuoteStrings",false)
5 Kommentare
Mathieu NOE
am 12 Sep. 2022
here a workaround :
function lines = my_readlines(filename)
% work around for earlier matlab releases (not having readlines)
lines = regexp(fileread(filename), '\r?\n', 'split');
if isempty(lines{end}); lines(end) = []; end %end of file correction
end
Siehe auch
Kategorien
Mehr zu Standard File Formats finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!