read_csv
Read data from a CSV file.
Syntax
[X,headers] = read_csv(fname,hdr,rr,delim) %Matlab & Octave #R to do
Description
Read data from a CSV file. The file is assumed to have a list of column names in the first line followed by lines of data. Each line must have the same number of elements as the first line. Empty cells are allowed. read_csv returns a structure array of strings with the field names taken from the headers. For a rectangular cell array of strings (nentries x nfields) use struct2cell(X) To convert a column containing only numbers to a vector of numbers, use: str2num(strvcat(X(:).field)) where field is the name of a field in structure X. To read only the header of the file, put hdr=1. If the CSV file does not have a header, put hdr=-1. In this case, the function will return a cell array with the contents of the file.
Inputs
Input var | Description | Default |
---|---|---|
fname | is the name of the CSV file. | N/A |
hdr | Switch for reading header. Set hdr=-1 if the file does not have a header, hdr = 0 (default) read file, hdr=1 read only header file | N/A |
rr | is a 1 by 2 array of start row to read (srr) & end row to read (err), i.e. [srr err]. The default is [] to read all rows in the file. If rr=[2 5] read_csv will start reading at row 2 and end reading at row 5. | N/A |
delim | Decimal ASCII character codes for table delimiters. Default values [44,9] for an ASCII comma (',') and ASCII horizontal tab ('\t') | N/A |
Outputs
Output var | Description | Units |
---|---|---|
X | N/A | |
headers | N/A |
Example
Download sensor_names.csv (1KB) to your working directory and type:
Matlab & Octave
S=read_csv('sensor_names.csv') % Returns: S is a structure with fields matching the names on the first line of sensor_names.csv.
R
# to do #Returns: S is a structure with fields matching the names on the first line of sensor_names.csv.
About
bugs@animaltags.org Last modified: 12 July 2017