horizontal concatenate a datetime structure to a table array
Show older comments
Hi I am trying to place a datetime array next to a table array
I am trying the follwing but getting an error.
Once they are concatenated I then wish to save as a text file..
tst=AUD_CAD.Date(1:length(table2array(AUD_CAD(2:end,2))));
mydates = datetime(tst,'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSSSSSSSSZ ', ...
'TimeZone','Europe/London','Format','yyyyMMddHHmmss ')
AUDCADn = horzcat(num2cell(mydates), table2array(AUD_CAD(2:end,2)));
Error using horzcat
Dimensions of arrays being concatenated are not consistent. Consider converting input
arrays to the same type before concatenating.
both structures are the same size.
The resulting tect file should look like the following.
2 Comments
Walter Roberson
on 1 May 2019
Do not use length of table2array there. Use height() of the table minus 1. Or better yet just index the Date variable at 1:end-1
Walter Roberson
on 1 May 2019
Instead of table2array(AUD_CAD(2:end,2)) use AUD_CAD{2:end, 2}
Accepted Answer
More Answers (1)
Peter Perkins
on 3 May 2019
The simplest way to add one variable to a table is just to assign it:
>> t = table(rand(3,1),rand(3,1))
t =
5×2 table
Var1 Var2
_______ _______
0.81472 0.09754
0.90579 0.2785
0.12699 0.54688
>> dt = datetime(2019,5,1:3)'
dt =
5×1 datetime array
01-May-2019
02-May-2019
03-May-2019
>> t.Time = dt
t =
5×3 table
Var1 Var2 Time
_______ _______ ___________
0.81472 0.09754 01-May-2019
0.90579 0.2785 02-May-2019
0.12699 0.54688 03-May-2019
There's an equivalent brace-subscripting syntax, but for one variable, dot is the way to go. This is all in the doc. The next simplest way, in recent versions (since R2018b IIRC) is to use addvars:
>> t = addvars(t,dt,'NewVariableNames','Time');
You can turn the workspace variable into a one-var table, and horzcat or assign, but the above are simpler.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!