Can writetable delimit strings using single-quotes?

조회 수: 4 (최근 30일)
FM . 2022년 4월 26일
편집: FM . 2022년 4월 26일
I am using writetable to export to CSV. All strings are delimited with double quotes. Is there any way to have strings delimited by single quotes? The help from "doc writetable" doesn't seem to provide a name-value pair to control this.

채택된 답변

Chunru 2022년 4월 26일
You may not change the quote string with writetable directly.
However, you can add a single quote to your string before writing.
% create a small table
str = ["A", "B", "C D"]';
num = [1 2 3]';
t = table(str, num)
t = 3×2 table
str num _____ ___ "A" 1 "B" 2 "C D" 3
% No quotation marks
writetable(t, 'test.txt')
type test.txt
str,num A,1 B,2 C D,3
% double quotation marks
writetable(t, 'test1.txt', 'QuoteStrings', true)
type test1.txt
str,num "A",1 "B",2 "C D",3
% single quotation marks
t.str = "'"+t.str+"'";
writetable(t, 'test2.txt')
type test2.txt
str,num 'A',1 'B',2 'C D',3
  댓글 수: 1
FM 2022년 4월 26일
편집: FM 님. 2022년 4월 26일
Thanks, Chunru. I'm actually dealing with cell arrays of char vectors. However, the result is the same when using writetable. I did consider embedding the desired quotes in the char vectors themselves, but was hoping that there was another way. Right now, I don't consider it worthwhile to write a function that tests each variable of a table to see if its a string or cell array of char vectors, then apply a function that adds single quotes. I guess I can always convert a table to a cell array, then use cellfun, then convert it back to a table with the same variable names.
Despite the fact that it is not worth it at the moment, I may cobble up such a function if I run into this need often enough. Thanks for sharing the idea and confirming that it is the only way. Maybe someone with deep inside knowledge will prove us both wrong -- one can hope!

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

추가 답변 (0개)


Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기





Community Treasure Hunt

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

Start Hunting!

Translated by