Read/Write CSV for Code Generation
63 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Michael
el 16 de Mayo de 2023
Comentada: FannoFlow
el 19 de Mayo de 2023
Is there a good way to read and/or write CSV files that is supported for code generation. I'd rather not write my own function but my searching leads me to believe that I am going to have to do this. Hopefully I am wrong.
For any Mathworks folks reading this question, if this isn't supported it should be (readmatix.m, readtable.m, etc.) on a future release.
0 comentarios
Respuesta aceptada
FannoFlow
el 17 de Mayo de 2023
To my knowledge, this does not exist.
And more specifically, I don't know how it could exist. The internal data of a csv could be anything, any size, and any length, and therefore not type safe enough for code generation.
I would recommend something like the following:
Más respuestas (1)
Allen
el 17 de Mayo de 2023
There are a number of options for reading CSV files. The following functions are few that I use and differ many in the type of workspace variable that you want after importing the data.
Here are a few of the inverse options for writing to file.
2 comentarios
Allen
el 17 de Mayo de 2023
Looks like my answer was only partially correct. Not all of the built-in read/write functions can handle CSV files. However, readtable and writetable do, which I use often and assumed the others did also.
Description
readtable determines the file format from the file extension:
- .txt, .dat, or .csv for delimited text files
- .xls, .xlsb, .xlsm, .xlsx, .xltm, .xltx, or .ods for spreadsheet files
- .xml for Extensible Markup Language (XML) files
- .docx for Microsoft® Word document files
- .html, .xhtml, or .htm for Hypertext Markup Language (HTML) files
For text and spreadsheet files, readtable creates one variable in T for each column in the file and reads variable names from the first row of the file. For XML files, readtable creates one variable in T for each element or attribute node detected as a table variable. Variable names correspond to element and attribute names. For Microsoft Word document files, readtable, by default, imports data from the first table in the document and creates one variable in T for each column in the file and reads variable names from the first row of the table. For HTML files, readtable, by default, imports data from the first <TABLE> element and creates one variable in T for each column in the file and reads variable names from the first row of the table.
By default, readtable creates variables that have data types that are appropriate for the data values detected in each column of the input file.
Description
writetable(T) writes table T to a comma delimited text file. The file name is the workspace variable name of the table, appended with the extension .txt. If writetable cannot construct the file name from the input table name, then it writes to the file table.txt.
For text and spreadsheet files, each column of each variable in T becomes a column in the output file. The variable names of T become column headings in the first line of the file.
For XML files, each column in T becomes an XML node in the output file. The variable names of T become the names of the element nodes in the output XML file.
writetable determines the file format based on the specified extension. The extension must be one of the following:
- .txt, .dat, or .csv for delimited text files
- .xls, .xlsm, or .xlsx for Excel® spreadsheet files
- .xlsb for Excel spreadsheet files supported on systems with Excel for Windows®
- .xml for Extensible Markup Language (XML) files
FannoFlow
el 19 de Mayo de 2023
Just for clarity, he was not asking about CSV's in matlab only.
He was asking for CSV methods that might support code generation, which is a separate case entirely.
The answer being that no, there are no built-in methods in matlab that directly import CSV files into some format that are also avaliable for code generation.
If code generation were not a requirement, readtable would certainly be the correct approach.
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!