Can writetable delimit strings using single-quotes?

2 次查看(过去 30 天)
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
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
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 中查找有关 Logical 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by