【问题标题】:Add datetime format to cell array in Matlab将日期时间格式添加到 Matlab 中的单元格数组
【发布时间】:2018-02-04 11:01:28
【问题描述】:

我有一个单元格数组,我称之为“表”,如下面的代码所示(但我的数组有更多行)。第 1 列包含字符串格式的日期。我想添加一个包含日期时间格式的日期的附加列。我做了以下工作,但它很慢。有哪些替代方案?

% Table that I have:

Table{1,1} = 'Stringdate';
Table{2,1} = '01.01.1999';
Table{3,1} = '02.01.1999';
Table{4,1} = '03.01.1999';
Table{5,1} = '04.01.1999';

% What I want to add:

Table{1, size(Table,2)+1} = 'Datetime';

for index = 2:length(Table)
    Table{index, size(Table,2)} = datetime(Table{index, 1});
end

【问题讨论】:

  • 代码在我的机器上完美运行?!如果它不适合您,请随时更改它,并提前感谢您的帮助

标签: arrays matlab datetime cell


【解决方案1】:

您可以一次性将datetime 应用于所有这些,并仅使用num2cell 和索引来实现与循环相同的结果。

Table(2:end,2) = num2cell(datetime(Table(2:end,1)));
%You might need to specify the InputFormat as well i.e.
%Table(2:end,2) = num2cell(datetime(Table(2:end,1),'InputFormat','dd.MM.yyyy'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 2011-04-19
    • 2022-01-18
    • 2021-10-16
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多