How do I "writetable" with a CSV file when table elements have newlines or double quotation marks?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 27 Juil 2017
Modifié(e) : MathWorks Support Team
le 6 Jan 2025
I have a table, "DataTable", in a MATLAB with elements that are strings containing line breaks, double quotes, and commas.
When I write the table from the CSV file, the table is not the same as the one I had originally. I get extra rows where the line breaks were and the quotes are not how they were originally.
Why does this happen and how can I write my table using a CSV file?
Réponse acceptée
MathWorks Support Team
le 6 Jan 2025
Modifié(e) : MathWorks Support Team
le 6 Jan 2025
Based on the definition of the CSV format, fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.
In order to write the original table to CSV format, you can add the "QuoteStrings" flag to "writetable". The flag is documented at the link below:
This is illustrated as follows:
>> writetable(DataTable, 'DataTable.csv','QuoteStrings',true);
Then, when reading from the CSV, you should explicitly tell "readtable" the format of the file. This option's workflow is documented at the link below:https://www.mathworks.com/help/matlab/ref/readtable.html#bvghccx
This is illustrated as follows:
>> opts = detectImportOptions('DataTable.csv');
>> T = readtable('DataTable.csv',opts)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!