Can writetable delimit strings using single-quotes?

11 Ansichten (letzte 30 Tage)
FM
FM am 26 Apr. 2022
Bearbeitet: FM am 26 Apr. 2022
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.

Akzeptierte Antwort

Chunru
Chunru am 26 Apr. 2022
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 Kommentar
FM
FM am 26 Apr. 2022
Bearbeitet: FM am 26 Apr. 2022
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by