Error using plot Invalid data argument (Data from Excel)
Show older comments
Hello,
I'm having a problem as I try to plot two columns of data from excel (temperature over time). I keep getting the following error. (Error using plot
Invalid data argument.) My code is below.
%%Use readtable for importing data from excel%%
WData = readtable('WeatherData1.xlsx');
%(1)Determine Temperature Variations throughout the year.
%Extact High temperatures
TempH = WData{2:366,2};
y = TempH;
%Extract Low temperatures
TempL = WData{2:366,4};
%Extract Avg Temperatures
Tempavg = WData{2:366,3};
%Extract the dates
Dates = WData{2:366,1};
x = Dates;
%plot the temperture variations over the year
plot(x,y)
23 Comments
Walter Roberson
on 9 Mar 2021
what shows up for class(Dates)? I would not be astonished if it were a cell array of character vectors: depending on how the time is represented in Excel, it might not be interpreted as a datetime.
Also which release are you using?
Nate Mays
on 9 Mar 2021
Walter Roberson
on 9 Mar 2021
and could you confirm that y is numeric?
R2018b is new enough that datetime plots were supported.
You could experiment with
plot(datenum(x), y)
Cris LaPierre
on 9 Mar 2021
Please share the full error message (all the red text).
Also, what data type is y?
Nate Mays
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
please show us the output of
class(x)
class(y)
Nate Mays
on 10 Mar 2021
Nate Mays
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
y is not numeric, y is cell!
Please attach an (extract from) 'WeatherData1.xlsx'
Nate Mays
on 10 Mar 2021
The only issue I see is that you are indexing 266 rows but you only have 265. What version of MATLAB are you using?
%%Use readtable for importing data from excel%%
WData = readtable('WeatherData1.xlsx');
%(1)Determine Temperature Variations throughout the year.
%Extact High temperatures
TempH = WData{2:365,2};
y = TempH;
%Extract Low temperatures
TempL = WData{2:365,4};
%Extract Avg Temperatures
Tempavg = WData{2:365,3};
%Extract the dates
Dates = WData{2:365,1};
x = Dates;
%plot the temperture variations over the year
plot(x,y)
Nate Mays
on 10 Mar 2021
Nate Mays
on 10 Mar 2021
Mathieu NOE
on 10 Mar 2021
hello
your code works fine with my R2020b
what do you get as table in WData ?
I get :
WData =
365×20 table
Date Temp____F_ Var3 Var4 DewPoin0___F_ Var6 Var7 Humidity____ Var9 Var10 SeaLevelPress___in_ Var12 Var13 Visibility__mi_ Var15 Var16 Wind__mph_ Var18 Var19 Precip_
___________ __________ ____ ____ _____________ ____ ____ ____________ ____ _____ ___________________ _____ _____ _______________ _____ _____ __________ _____ _____ _______
01-Jan-2011 45 37 29 26 15 9 61 43 24 30.35 30.22 29.98 10 10 10 21 10 25 0
02-Jan-2011 49 37 25 20 16 11 74 49 24 30.43 30.33 30.25 10 10 10 10 4 14 0
03-Jan-2011 60 45 30 33 26 19 69 50 30 30.23 30.13 30.01 10 10 10 22 10 26 0
04-Jan-2011 57 49 40 45 40 33 83 71 59 30.14 30.06 29.98 10 10 10 13 5 15 0
Nate Mays
on 10 Mar 2021
Mathieu NOE
on 10 Mar 2021
and WData dimensions are also 365 x 20 on your side ?
Nate Mays
on 10 Mar 2021
Nate Mays
on 10 Mar 2021
Nate Mays
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
The file you posted did not have quotes around the numeric data though ??
Nate Mays
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
You would get that if you tried to read a header. You might need to use detectImportOptions first which was not yet automatic in your release.
Nate Mays
on 10 Mar 2021
Accepted Answer
More Answers (0)
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!