Trouble using csvread to extract data for plotting

7 views (last 30 days)
I am trying to plot data from an Excel file using a MacBook laptop, but I am having issues using csvread. I initially tried using xlsread to extract the data from a .xlsx file but this didn't work (I understand this is something that doesn't work on Mac OS?) so now I have saved my data in a .csv file. It is saved as the 'Comma Separated Values' option rather than the 'CSV UFT-8 (Comma delimited)' option. The data is two columns, A1:B173, and it is ALL in numerical format.
The MATLAB script I wrote is:
vals = 'sensitivityplot2.csv';
xrange = 'B1:B173';
yrange = 'A1:A173';
X = csvread(vals,xrange);
Y = csvread(vals,yrange);
figure()
plot(X,Y)
The output error I get is:
Error using dlmread (line 143)
HeaderLines must be integer-valued.
Error in csvread (line 47)
m=dlmread(filename, ',', r, c);
Error in plotgraph (line 5)
X = csvread(vals,xrange);
Any help would be very much appreciated!

Answers (1)

Cam Salzberger
Cam Salzberger on 23 Oct 2017
The xlsread function does work on Macs, just in basic mode since it cannot communicate with Microsoft Excel over ActiveX. When in basic mode, you cannot specify the acceptable ranges, which is probably the source of that error.
csvread does not take in range arguments in Excel-format. You'll want to use this syntax for the csvread call:
M = csvread(filename,R1,C1,[R1 C1 R2 C2])
Remember that the R1, C1, etc. values are offsets, not indexes, so the full file would have an R1, C1 offset of 0.
-Cam

Community Treasure Hunt

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

Start Hunting!