string output in text-file without " " (quotation notes). MATLAB BUG? comma in string effects quotation notes in .txt-file

Hi guys!
I noticed something strange in MATLAB. Maybe anybody knows this problem.
I would like to write a string-vector into a .txt-file.
If my string-vector contains a comma "," so my .txt.file begins with an quotation mark and end with one "....."
But i need a .txt-file without quotation marks, only with the strings, which maybe contains commas...
example 1 without comma in string and so without quotation marks in .txt-file:
Name1 = "Walter";
Gewicht1 = 55;
testvektor(1,1) = "Walter hat " + " / Hunger "+"";
testvektor(2,1) = "Gewicht" + ": " + Gewicht1;
fid = fopen('Textdatei.txt','wt');
writematrix(testvektor,'Textdatei.txt',"FileType","text")
fclose(fid);
Output example 1:
Walter hat / Hunger
Gewicht: 55
example 2 with comma in string and so with quotation marks in .txt-file:
Name1 = "Walter";
Gewicht1 = 55;
testvektor(1,1) = "Walter hat " + " / Hunger "+",";
testvektor(2,1) = "Gewicht" + ": " + Gewicht1;
fid = fopen('Textdatei.txt','wt');
writematrix(testvektor,'Textdatei.txt',"FileType","text")
fclose(fid);
Output example 2:
"Walter hat / Hunger "
"Gewicht: 55"
Thank you for helping guys!
Hope you know the issue!
Best regards,
Maxim

댓글 수: 2

It is not advisable to fopen(), writematrix(), fclose(). writematrix() does not operate on a file identifier: it opens and closes the file as needed. The extra fopen() justs risks glitches, unnecessarily.

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

 채택된 답변

You can use "QuoteStrings","none" in your call to writematrix() to avoid writing the double-quotes in either case:
Name1 = "Walter";
Gewicht1 = 55;
testvektor(1,1) = "Walter hat " + " / Hunger "+"";
testvektor(2,1) = "Gewicht" + ": " + Gewicht1;
testvektor
testvektor = 2×1 string array
"Walter hat / Hunger " "Gewicht: 55"
writematrix(testvektor,'Textdatei.txt',"FileType","text","QuoteStrings","none")
show_file_contents('Textdatei.txt');
Walter hat / Hunger Gewicht: 55
testvektor(1,1) = "Walter hat " + " / Hunger "+",";
testvektor(2,1) = "Gewicht" + ": " + Gewicht1;
testvektor
testvektor = 2×1 string array
"Walter hat / Hunger ," "Gewicht: 55"
writematrix(testvektor,'Textdatei.txt',"FileType","text","QuoteStrings","none")
show_file_contents('Textdatei.txt');
Walter hat / Hunger , Gewicht: 55
Or you can use fprintf() instead of writematrix():
fid = fopen('Textdatei.txt','w');
fprintf(fid,'%s\n',testvektor);
fclose(fid);
show_file_contents('Textdatei.txt');
Walter hat / Hunger , Gewicht: 55
function show_file_contents(fn)
fid = fopen(fn);
data = fread(fid,'*char').';
fclose(fid);
disp(data);
end

댓글 수: 1

Exactly. In a comma separated text file, the occurence of one comma inside the strings would create an ambiguous file. To avoid this double quotes are added as default when commas are found.
This is a smart feature, which can be confusing. This is a typical effect of applying heuristics to guess, what the users wants.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 3월 12일

댓글:

2022년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by