Read/Write CSV for Code Generation

70 次查看(过去 30 天)
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.

采纳的回答

FannoFlow
FannoFlow 2023-5-17
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:
  1 个评论
Michael
Michael 2023-5-17
That's what I was realizing after thinking about the data types at bit. I ended up building my own with fscanf.

请先登录,再进行评论。

更多回答(1 个)

Allen
Allen 2023-5-17
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 个评论
Allen
Allen 2023-5-17
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
T = readtable(filename) creates a table by reading column oriented data from a file.
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(T,filename) writes to a file with the name and extension specified by filename.
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
FannoFlow 2023-5-19
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.

请先登录,再进行评论。

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by