How to convert one column from string to datetime

Hi everyone
I have a n by 3 matrix. The second column is date in string format. I need to convert the second column to datetime format "YYYY-MM-DD" and return the other 2 columns untouched. I thought this would work but I am getting an error:
ABC(:,2) = datetime(ABC(:,2));
The error is:
Error using datetime
Input data must be a numeric array, a string array, a cell array containing character vectors,
or a char matrix.
How can I fix my code?
Thank you

4 件のコメント

Stephen23
Stephen23 2024 年 2 月 18 日
"How can I fix my code?"
Please upload your data in a MAT file by clicking the paperclip button.
Manny
Manny 2024 年 2 月 18 日
編集済み: Manny 2024 年 2 月 18 日
Here is a few entries from the second column:
>> ABC(:,2)
ans =
2443×1 table
TRADE_DATE
______________
{'2014-06-03'}
{'2014-06-11'}
{'2014-06-19'}
{'2014-06-25'}
{'2014-06-26'}
Manny
Manny 2024 年 2 月 18 日
編集済み: Manny 2024 年 2 月 18 日
@Stephen23, thanks for the reply. I am new to Matlab. I am not really sure what you mean/need. Are you asking for this? The problem is CIV table.
Dyuman Joshi
Dyuman Joshi 2024 年 2 月 18 日
Save the table as an excel file using writetable and upload it here using the paperclip button.

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

 採用された回答

Dyuman Joshi
Dyuman Joshi 2024 年 2 月 18 日

0 投票

If you are using R2018b or a later version, you can use convertvars -
%Random data for example
ABC = table(rand(5,1), {'2014-06-03';'2014-06-11';'2014-06-19';'2014-06-25';'2014-06-26'})
ABC = 5×2 table
Var1 Var2 ________ ______________ 0.018163 {'2014-06-03'} 0.91476 {'2014-06-11'} 0.90892 {'2014-06-19'} 0.11466 {'2014-06-25'} 0.94165 {'2014-06-26'}
%Convert the 2nd column data to datetime
ABC = convertvars(ABC, 2, @(x) datetime(x, 'InputFormat', 'yyyy-MM-dd'))
ABC = 5×2 table
Var1 Var2 ________ ___________ 0.018163 03-Jun-2014 0.91476 11-Jun-2014 0.90892 19-Jun-2014 0.11466 25-Jun-2014 0.94165 26-Jun-2014

2 件のコメント

Manny
Manny 2024 年 2 月 18 日
Thank you so much! it worked
Dyuman Joshi
Dyuman Joshi 2024 年 2 月 18 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2024 年 2 月 18 日

コメント済み:

2024 年 2 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by