csvread doesn't work, I just get errors.

48 views (last 30 days)
Hank Gunderson
Hank Gunderson on 24 Jul 2018
Commented: Hank Gunderson on 27 Jul 2018
I'm trying to import data from a data logger in csv format, this site says
"M = csvread('FILENAME',R,C)" is how you do that but I tried "m=csvread('LOGGER02.CSV',C5); disp(m);", but all I get is
Undefined function or variable 'C5'.
Error in untitled (line 1) m=csvread('LOGGER02.CSV',C5);
whats the real way to read csv files, assuming Matlab can read/import them? I'm using R2017b

Answers (2)

Aarti Dwivedi
Aarti Dwivedi on 24 Jul 2018
R = 1
C5 = 1
m=csvread('LOGGER02.CSV',R, C5); disp(m);
The error is pretty straightforward that your variable definitions are missing. Just saying C5 doesn't mean that the cell address is C5. C5 is the name of the variable in which you will store the column offset.
  19 Comments
Walter Roberson
Walter Roberson on 25 Jul 2018
The simplest way is
t = readtable('LOGGER02.CSV', 'HeaderLines', 1, 'ReadVariableName', false);
temperature = t{:,2}; %or as appropriate
humidity = t{:,4}; %or as appropriate
If you have R2016b or later, you can use detectImportOptions and set the SelectedVariables property to cause it to throw away everything else when you use readtable()
For older versions of MATLAB, before R2013b, then probably it would be best to use fopen()/textscan()/fclose() . This requires knowing the format of the columns you are skipping.

Sign in to comment.


Image Analyst
Image Analyst on 24 Jul 2018
If "C5" is the column/row you see when you open it in Excel, then row = 5 and column = 3, and csvread() expects integers for offsets, not actual row or column numbers (i.e. 4 and 2):
Starting row offset, specified as a nonnegative integer. The first row has an offset of 0.
Starting column offset, specified as a nonnegative integer. The first column has an offset of 0.
so try
m=csvread('LOGGER02.CSV', 4, 2);

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!