필터 지우기
필터 지우기

Import date data from excel

조회 수: 19 (최근 30일)
Sayantan Sahu
Sayantan Sahu 2018년 1월 18일
답변: Peter Perkins 2018년 1월 18일
I have an excel sheet with two columns. The first column has the date in the format mmm-yy and the second column has the values.
Column A Column B Jan-05 Value Feb-05 Value Mar-05 Value
I imported the dates successfully as,
month_year=xlsread('Filename','A2:A133'); formatOut = 'mmm-yy'; month_year = datestr(month_year,formatOut)
The output month_year is of 'char' type of 132x6.
I wish to plot with x-axis being month_year and y-axis the corresponding values. So I write something like,
month_year=datenum(month_year,'mmm-yy') plot(month_year, data) datetick('x','mmmyyyy','keepticks','keeplimits')
However in the process all the dates in the x-axis are now 'Jan 00'
How do I solve this ?

채택된 답변

Peter Perkins
Peter Perkins 2018년 1월 18일
Unless you are using an older version of MATLAB, you are likely much better of using readtable to read your data into a table, converting the date text into a datetime (if necessary, depends on what version of MATLAB you're using and how the spreadsheet is set up), and plotting the vales vs. the datetimes.
Let's assume readtable gives you a table something like this:
>> t = table({'Jan-05';'Feb-05';'Mar-05'},[1;2;3],'VariableNames',{'A' 'B'})
t =
3×2 table
A B
________ _
'Jan-05' 1
'Feb-05' 2
'Mar-05' 3
Do this:
>> t.A = datetime(t.A,'InputFormat','MMM-yy')
t =
3×2 table
A B
___________ _
01-Jan-2005 1
01-Feb-2005 2
01-Mar-2005 3
>> plot(t.A,t.B)
If you need to tinker with the x axis ticks or labels, here's where to find them:
>> h = gca;
>> h.XAxis
ans =
DatetimeRuler with properties:
Limits: [01-Jan-2005 05-Mar-2005]
TickValues: [01-Jan-2005 15-Jan-2005 29-Jan-2005 12-Feb-2005 26-Feb-2005]
TickLabelFormat: 'MMM dd'
Show all properties

추가 답변 (1개)

KSSV
KSSV 2018년 1월 18일
Use this:
[num,txt,raw] = xlsread(myfile) ;
  댓글 수: 1
Sayantan Sahu
Sayantan Sahu 2018년 1월 18일
The num file imports the dates as integer numbers. In that case how would I plot it on x-axis so that it shows something like 'Jan-05'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by