How to convert time column into a regular array?

54 ビュー (過去 30 日間)
Anjali Mishra
Anjali Mishra 2022 年 1 月 21 日
コメント済み: Anjali Mishra 2022 年 1 月 21 日
I have a table with first column as time series, and other columns as other parameters that changes with time. I want to convert the time series into an array for plaotting. I tried following code:
A_time = A(:,1);
A_time_array = table2array(A_time);
This method works for all the numeric columns. However, it errors out in the time column. Can someone suggest a better way?
  1 件のコメント
Stephen23
Stephen23 2022 年 1 月 21 日
"Can someone suggest a better way?"
Convert the "time series" to datetime objects and plot using them.

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

採用された回答

Star Strider
Star Strider 2022 年 1 月 21 日
If the time series are a datetime array, they can easily be plotted as funcitons of that variable. It would be best to convert them to a datetime array if they are not already.
The other (less-desirable) option is to convert them to a datenum array and then use the datetick function.
.
  2 件のコメント
Star Strider
Star Strider 2022 年 1 月 21 日
Anjali Mishra’s Answer became this Comment —
Thank you Star Stider. Here is the scenario. A is a table with first column as time and other columns as other parameters. A am
>> A(:,1)
A(:,1)=
4×1 cell array
{[2021-11-14 19:16:02.328]}
{[2021-11-14 19:16:02.430]}
{[2021-11-14 19:16:02.533]}
{[2021-11-14 19:16:02.640]}
>> temperature = table2array(A(:,2));
This is expected to be x-axis on the plot with temperature
>> plot(A(:,1), temperature, 'r*')
results into A_time being invalid arguments.
Is there a way to keep this format of time as in 2021-11-14 19:16:02.328?
Star Strider
Star Strider 2022 年 1 月 21 日
Those dates and times must be present in the file as character or string arrays. It is not possible to work with them as they are presented, so having the original file would help.
Assuming that they are (or can be made to be) read as character arrays or string arrays, the conversion is straightforward —
A = {{['2021-11-14 19:16:02.328']}
{['2021-11-14 19:16:02.430']}
{['2021-11-14 19:16:02.533']}
{['2021-11-14 19:16:02.640']}}
A = 4×1 cell array
{1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell}
Aa = datetime([A{:}], 'InputFormat','yyyy-MM-dd HH:mm:ss.SSS', 'Format','yyyy-MM-dd HH:mm:ss.SSS').'
Aa = 4×1 datetime array
2021-11-14 19:16:02.328 2021-11-14 19:16:02.430 2021-11-14 19:16:02.533 2021-11-14 19:16:02.640
Using the same format descriptor string for both 'InputFormat' and 'Format' causes the output to match the input.
.

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

その他の回答 (1 件)

Jon
Jon 2022 年 1 月 21 日
t = seconds(A_time.Time - A_time.Time(1))
  1 件のコメント
Anjali Mishra
Anjali Mishra 2022 年 1 月 21 日
This also works equally well.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by