delete consecutive commas in txt file

조회 수: 4 (최근 30일)
george korris
george korris 2022년 9월 12일
댓글: george korris 2022년 9월 13일
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
Stephen23
Stephen23 2022년 9월 12일
What should happen with lines that only contain commas: do you want to keep them, or remove them?
george korris
george korris 2022년 9월 12일
@Stephen23thank you for your time.
remove them!

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 9월 12일
편집: Star Strider 2022년 9월 12일
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')
M1 = 8×9
0.0044 0.0415 0.0508 0.1185 0.1397 0.2522 0.3003 0.4102 0.6981 0.0361 0.1039 0.1559 0.1819 0.2685 0.4436 0.5813 0.8723 1.2182 0.0766 0.2214 0.2013 0.3533 0.5310 0.9182 1.1550 1.4976 1.9698 0.0721 0.5354 0.6176 0.6157 1.2760 1.7109 1.9629 2.4014 2.9319 0.2162 0.2679 1.3769 1.7134 2.1104 2.6804 3.0724 3.2769 4.0396 1.4989 1.6092 2.2079 2.4976 2.7068 3.8842 3.8599 4.3714 4.6691 1.7589 2.4418 2.3176 3.3207 3.0543 4.3723 4.3590 4.6367 4.8807 0.1454 2.1400 2.5672 3.5504 3.3989 4.4617 4.5724 5.0063 4.9371
.
  댓글 수: 4
george korris
george korris 2022년 9월 12일
Thank you so much @Star Strider!!! You are amazing!!!!!
Star Strider
Star Strider 2022년 9월 12일
As always, my pleasure!
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Simon Chan
Simon Chan 2022년 9월 12일
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
iwant = 8×9
0.0044 0.0415 0.0508 0.1185 0.1397 0.2522 0.3003 0.4102 0.6981 0.0361 0.1039 0.1559 0.1819 0.2685 0.4436 0.5813 0.8723 1.2182 0.0766 0.2214 0.2013 0.3533 0.5310 0.9182 1.1550 1.4976 1.9698 0.0721 0.5354 0.6176 0.6157 1.2760 1.7109 1.9629 2.4014 2.9319 0.2162 0.2679 1.3769 1.7134 2.1104 2.6804 3.0724 3.2769 4.0396 1.4989 1.6092 2.2079 2.4976 2.7068 3.8842 3.8599 4.3714 4.6691 1.7589 2.4418 2.3176 3.3207 3.0543 4.3723 4.3590 4.6367 4.8807 0.1454 2.1400 2.5672 3.5504 3.3989 4.4617 4.5724 5.0063 4.9371
  댓글 수: 1
george korris
george korris 2022년 9월 12일
Hi @Simon Chan when i run your code in my pc i get the following error:
Unrecognized function or variable 'readlines'.
Thank you for your time!

댓글을 달려면 로그인하십시오.


Mathieu NOE
Mathieu NOE 2022년 9월 12일
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
Mathieu NOE
Mathieu NOE 2022년 9월 12일
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
george korris
george korris 2022년 9월 13일
Thank you very much!!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by