How can I concatenate a "datetime" column to a matrix of numbers?

40 ビュー (過去 30 日間)
Srh Fwl
Srh Fwl 2020 年 7 月 21 日
編集済み: Srh Fwl 2020 年 7 月 21 日
I have imported a table from an external file via textscan. For example:
A = rawCellColumns2(:, 1);
B = cell2mat(rawNumericColumns2(:, 1));
C = cell2mat(rawNumericColumns2(:, 2));
D = cell2mat(rawNumericColumns2(:, 3));
E = cell2mat(rawNumericColumns2(:, 4));
A is "datetime" in this form:
2019-07-10 22:00:00
B, C, D, and E are all numbers.
I now want to make a new table (Z) by concatenating A - E. However, I get the error below because of the datetime column (I have no problem concatenating the columns of numbers by doing B(:), C(:), D(:), E(:)):
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
People have addressed this problem here at Matlab Answers, but as a bit of a newbie, I would be very grateful if someone could please provide a newbie-level explanation. I am using 2016a. Thank you very much for any advice.

採用された回答

Steven Lord
Steven Lord 2020 年 7 月 21 日
You cannot concatenate together data of types datetime and double without converting the datetime array to double or the double array to datetime. But that's probably not what you want.
If you were using release R2016b or later I would suggest creating a timetable array from your data, but you indicated you're using R2016a. If you cannot upgrade to release R2016b or later I think you're probably going to want to create a table array instead. [The table data type was introduced in release R2013b.] Let's make some sample data.
v = (0:4).';
dt = datetime('today') + days(v);
x = v.^2;
Now let's build the table.
T = table(dt, x)
See the documentation for table to learn how to work with data stored in a table.
doc table
One example: you can extract the row of the table corresponding to x = 4.
T(T.x == 4, :)
  1 件のコメント
Srh Fwl
Srh Fwl 2020 年 7 月 21 日
編集済み: Srh Fwl 2020 年 7 月 21 日
Thank you very much, Steven for taking time to provide that clear explanation. Unfortunately, I can't upgrade my Matlab version at present. I'll experiment and will come back (probably tomorrow) to accept your answer if I am successful.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by