Categories &

Functions List

Function Reference: csv2table

datatypes: tbl = csv2table (filename)
datatypes: tbl = csv2table (filename, Name, Value)

Load a CSV file into a table.

tbl = csv2table (filename) creates a table tbl by reading the data in CSV file specified by filename, which can be either character vector or a string scalar. If the CSV file was saved by the table2csv method, then it reads the custom comment line in the first element of the CSV file and reconstructs the table as specified including variable types, variable names, and possibly any nested tables or structures it may contain. If no such special comment is found, then it treats the CSV file as simple columnar data and loads with the following default options.

  1. The first column is considered as RowNames and it is converted to cell array of character vectors and it is subsequently removed from the remaining data contained in the CSV file. To change the default behavior, you need to specify the ReadRowNames and RowNamesColumn paired arguments accordingly.
  2. The first line is treated as a header containing the variable names of the table. Any numeric values in the header line are converted to character vectors and all variable names are automatically modified to valid Octave variable names. To change the default behavior, you need to specify the ReadVariableNames, VariableNamingRule, and VariableNamesLine paired arguments accordingly.
  3. The data type of each column in the remaining data is automatically detected according to its contents. Consequently, text is converted to character vectors, datetime and duration strings are converted to datetime and duration arrays, respectively, and hexadecimal strings are converted to the smallest integer type that can represent all variable values. To change the default behavior, you need to specify the TextType, DatetimeType, DurationType, and HexType accordingly.

tbl = csv2table (filename, Name, Value) specifies optional parameters for creating the table tbl with the following Name-Value paired arguments.

NameValue
'NumHeaderLines'A positive integer scalar value specifying the number of rows to omit reading from the CSV file. If the CSV contains the custom comment line in its first element, then the 'NumHeaderLines' applies to the top number of rows to remove from the created table. If CSV is a generic case, then 'NumHeaderLines' specifies the number of lines to omit parsing from the CSV file itself.
'VariableNames'A cell array of character vectors or a string array specifying the names of the variables of the created table. The names must be unique but not necessarily valid variable names. If not empty, it overrides any variable names extracted from the CSV file. This applies both to custom and generic CSV files.
'ReadVariableNames'A logical scalar specifying whether to parse or not the CSV files for variable names. If your CSV file only contains data, set 'ReadVariableNames' to false so that csv2table parses all lines as data rows in the returned table. Variable names will be automatically generated to the default style as done by table, unless otherwise specified by the 'VariableNames' paired argument. The default value is true.
'VariableNamingRule'A character vector or a string scalar specifying whether the variable names should be modified to valid Octave variable names or the original names should be preserved. Valid options are "modify" and "preserve". By default, csv2table modifies the parsed variable names.
'VariableNamesLine'A nonnegative integer scalar value specifying the line number in the CSV file, which should be parsed for variable names. This only applies if the 'ReadVariableNames' option is true. The specified line is subsequently removed from the remaining data contained in the CSV file. If 'VariableNamesLine' is set to zero, then it is equivalent to setting 'ReadVariableNames' to false.
'VariableTypes'A cell array of character vectors or a string array specifying the data type of the variables of the created table. The number of elements must much the number of variable in the table. This optional argument only has an effect on generic CSV files. When specified, it overrides any other data type specification or automatic detection by the csv2table function.
'VariableUnitsLine'A nonnegative integer scalar value specifying the line number in the CSV file, which should be parsed for variable units. The specified line is subsequently removed from the remaining data contained in the CSV file.
'VariableDescriptionsLine'A nonnegative integer scalar value specifying the line number in the CSV file, which should be parsed for variable descriptions. The specified line is subsequently removed from the remaining data contained in the CSV file.
'ReadRowNames'A logical scalar specifying whether to parse or not the CSV files for row names. If your CSV file only contains data, set 'ReadRowNames' to false so that csv2table parses all columns as data columms in the returned table. The default value is true.
'RowNamesColumn'A nonnegative integer scalar value specifying the column number in the CSV file, which should be parsed for row names. This only applies if the 'ReadRowNames' option is true. The specified column is subsequently removed from the remaining data contained in the CSV file. If 'RowNamesColumn' is set to zero, then it is equivalent to setting 'ReadRowNames' to false. Note that the values in the column specified by 'RowNamesColumn' must be unique, otherwise csv2table will return an error.
'TextType'A character vector or a string scalar specifying whether the text data in the CSV file should be stored in the table as character vectors or string arrays. Valid options are "char" and "string". By default, csv2table stores text data as character vectors.
'DatetimeType'A character vector or a string scalar specifying whether the datetime strings found in the CSV file should be stored in the table as datetime arrays or as text data. Valid options are "datetime" and "text". By default, csv2table stores datetime strings as datetime arrays. If "text" is specified, then the data type depends on the 'TextType' option.
'DurationType'A character vector or a string scalar specifying whether the duration strings found in the CSV file should be stored in the table as duration arrays or as text data. Valid options are "duration" and "text". By default, csv2table stores duration strings as duration arrays. If "text" is specified, then the data type depends on the 'TextType' option.
'HexType'A character vector or a string scalar specifying whether the hexadecimal text found in the CSV file should be stored as a suitable integer type, "auto" (default), as unaltered input text, "text", (in which case the data type depends on the 'TextType' option), or as any of the integer types supported by Octave. Valid options are "auto", "text", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", and "uint64".

See also: array2table, struct2table, table

Source Code: csv2table