MATLAB Answers

horizontal concatenate a datetime structure to a table array

26 views (last 30 days)
Charles on 1 May 2019
Answered: Peter Perkins on 3 May 2019
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..
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.


Walter Roberson
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
Walter Roberson on 1 May 2019
Instead of table2array(AUD_CAD(2:end,2)) use AUD_CAD{2:end, 2}

Sign in to comment.

Accepted Answer

dpb on 1 May 2019
I can't tell which data it is you're trying to piece together, but from the OrginalData.mat you posted in the previous Q? on dates, looks to me like all you have to do is something like
tAUD=array2table(datetime(GBP_USD.Date,'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSSSSSSSSZ', ...
'TimeZone','Europe/London','Format','yyyyMMddHHmmss'),'VariableNames',{'Date'}); % reformat the date
tAUD.Open=GBP_USD.Open; % save the desired variables
tAUD.Close=GBP_USD.Close; % from the existing to the new
tAUD=tAUD(2:end,:); % if you don't want the first record, get rid of it
Much like the other problem you posted very shortly after this, direct manipulation of the table object is FAR the simplest route generally...


Sign in to comment.

More Answers (1)

Peter Perkins
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
>> 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.


Sign in to comment.

Sign in to answer this question.