Plotting date and time

28 ビュー (過去 30 日間)
Giulia
Giulia 2023 年 1 月 12 日
編集済み: Cris LaPierre 2023 年 1 月 20 日
I am new to Matlab and I need to plot two columns of data from a xlsx file. One of the two columns contains date and time (04/07/2022 18:00).
I keep receiving errors everytime I try to turn the date data into a readable format for Matlab:
Can anyone help me in understanding how to read date and time data in Matlab? These are the errors I receive.
Thank you.
>> datestr(datenum(tdata(:,1),'dd.mm.yyyy')
datestr(datenum(tdata(:,1),'dd.mm.yyyy')
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Did you mean:
>> datestr(datenum(tdata(:,1),'dd.mm.yyyy'))
Error using datenum
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed to convert from text to date number.
>> t = datetime('tdata')
Error using datetime
Could not recognize the date/time format of 'tdata'. You can specify a format using the 'InputFormat' parameter.
If the date/time text contains day, month, or time zone names in a language foreign to the 'en_US' locale, those
might not be recognized. You can specify a different locale using the 'Locale' parameter.
  1 件のコメント
Giulia
Giulia 2023 年 1 月 16 日
Thanks a lot!!

サインインしてコメントする。

回答 (2 件)

Cris LaPierre
Cris LaPierre 2023 年 1 月 12 日
編集済み: Cris LaPierre 2023 年 1 月 20 日
Create a datetime variable from your date and time information, and then just plot. The axes will automatically adjust to handle the datetime variable.
If you use readtable to load your excel data, chances are it will automatically load the date and time into a datetime variable already. See this page on how to access data in a table.
If you share your spreadsheet, we can provide more specific help.
  2 件のコメント
Cris LaPierre
Cris LaPierre 2023 年 1 月 12 日
編集済み: Cris LaPierre 2023 年 1 月 20 日
You may need to specify the input format to datetime. However, first consider using the 'ConvertFrom','excel' name value pair.
The functions datenum and datestr are older functions that should be avoided when possible.
Giulia
Giulia 2023 年 1 月 18 日
thanks a lot!!!

サインインしてコメントする。


Bora Eryilmaz
Bora Eryilmaz 2023 年 1 月 12 日
編集済み: Bora Eryilmaz 2023 年 1 月 12 日
tdata = { ...
'04/07/2022 18:00', ...
'05/08/2022 19:30', ...
'06/08/2022 11:30'
};
ydata = [10, 20, 15];
tnum = datenum(tdata); % Numeric representation of dates
tstr = datestr(tnum, 'dd.mm.yyyy'); % Extract date strings in desired format
tdate = datetime(tstr) % Convert to datetime object for plotting
tdate = 3×1 datetime array
07-Apr-2022 08-May-2022 08-Jun-2022
plot(tdate, ydata)
  1 件のコメント
Giulia
Giulia 2023 年 1 月 16 日
thanks a lot!!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by