MATLAB Answers

horizontal concatenate a datetime structure to a table array

조회 수: 50(최근 30일)
Charles
Charles 1 May 2019
답변: Peter Perkins 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..
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
Walter Roberson 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 1 May 2019
Instead of table2array(AUD_CAD(2:end,2)) use AUD_CAD{2:end, 2}

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 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...

  댓글 수: 0

댓글을 달려면 로그인하십시오.

추가 답변(1개)

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

  댓글 수: 0

댓글을 달려면 로그인하십시오.

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by