horizontal concatenate a datetime structure to a table array
이전 댓글 표시
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
Walter Roberson
2019년 5월 1일
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
2019년 5월 1일
Instead of table2array(AUD_CAD(2:end,2)) use AUD_CAD{2:end, 2}
채택된 답변
추가 답변 (1개)
Peter Perkins
2019년 5월 3일
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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!