Can writetable delimit strings using single-quotes?

3 ビュー (過去 30 日間)
FM
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
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 Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by