이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Error using plot Invalid data argument (Data from Excel)
조회 수: 2 (최근 30일)
이전 댓글 표시
Nate Mays
2021년 3월 9일
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
Walter Roberson
2021년 3월 9일
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
2021년 3월 9일
I attaced an image of what shows up and I'm currenty using R2018b from my school. In excell its set as Date.
Walter Roberson
2021년 3월 9일
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
2021년 3월 9일
Please share the full error message (all the red text).
Also, what data type is y?
Nate Mays
2021년 3월 10일
Error using plot
Invalid data argument.
Error in WeatherExtractionScript (line 21)
plot(x,y)
Walter Roberson
2021년 3월 10일
y is not numeric, y is cell!
Please attach an (extract from) 'WeatherData1.xlsx'
Nate Mays
2021년 3월 10일
Here is the document. I checked the excel document to make sure that the column with the dates are set as dates and the columns with the weather data is set as numbers.
Cris LaPierre
2021년 3월 10일
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');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
%(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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/545227/image.png)
Nate Mays
2021년 3월 10일
I'm using R2018b, I changed the 366b to 365 but still getting the same error. is that the only thing you changed?
Mathieu NOE
2021년 3월 10일
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
2021년 3월 10일
My WData table looks the same as yours, so you also got a plot as well from the code.
Nate Mays
2021년 3월 10일
I figured out the issue, My WData had quotations around my number data and after I removed that, I recieved my grpahs and it was plotted correctly
Walter Roberson
2021년 3월 10일
The file you posted did not have quotes around the numeric data though ??
Walter Roberson
2021년 3월 10일
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.
채택된 답변
ANKUR KUMAR
2021년 3월 10일
You can generate dates as per the values in the excel sheet. This won't throw an error for dimension mismatch.
[~, ~, weather_data] = xlsread('WeatherData1.xlsx','Sheet1','A1:B367');
weather_data(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),weather_data)) = {''};
weather_data=weather_data(3:end,:);
time=cellfun(@(x) datetime(datestr(datenum(x,'dd-mmm-yy'))) ,weather_data(:,1),'uni',0);
temp=weather_data(:,2);
plot(cat(1,time{:}),cell2mat(weather_data(:,2)))
Hope this helps.
댓글 수: 1
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)