Converting time data to plot

조회 수: 17 (최근 30일)
Ryan McBurney
Ryan McBurney 2019년 9월 10일
편집: Adam Danz 2019년 9월 16일
I imported an excel file (xlsread) and the time data turned into decimal form. I wrote the following code get a what I think is called a character array of just times.
um_time = num(:,1);
theDate = char(datetime(num_time,'ConvertFrom','excel')); % or 'excel1904'
theTimes = theDate(:,end-7:end);
The output for theTimes variable looks like this
val =
19:17:10
19:17:10
19:17:10
19:17:10
....
The problem that I have is that I can't use the variable theTimes in a plot function.
How can I turn theTimes values into a column vector of values that I can use to plot?

답변 (1개)

Adam Danz
Adam Danz 2019년 9월 10일
편집: Adam Danz 2019년 9월 16일
After you convert to datetime, keep it in datetime format. There's no need to convert it to char.
From r2016b onward, you can use datetime objects in plot().
datetime(num_time,'ConvertFrom','excel')
Here's several of examples
[addendum]
If you're reading the dates in from excel you can read them in as datetime.
m = readmatrix('Dates.xlsx','OutputType','datetime');
  댓글 수: 2
Ryan McBurney
Ryan McBurney 2019년 9월 12일
I figured out a different approach.
I used readtable() instead of xlsread. I used this code as well:
data = readtable(filename,'Range','A22:Y432');
data.Time = datetime(datevec(data.Time),'Format','HH:mm:ss.SSS');
The second line in the code solved my problem.
Thanks for the nudge in the right direction.
Adam Danz
Adam Danz 2019년 9월 12일
편집: Adam Danz 2019년 9월 13일
I just updated my answer to add another approach similar to yours.
If you're going to use readtable(), you could use the DateTimeType name-value input instead of doing the conversion in your comment above.

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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by