horizontal concatenate a datetime structure to a table array

8 ビュー (過去 30 日間)
Charles
Charles 2019 年 5 月 1 日
回答済み: Peter Perkins 2019 年 5 月 3 日
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 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
Walter Roberson 2019 年 5 月 1 日
Instead of table2array(AUD_CAD(2:end,2)) use AUD_CAD{2:end, 2}

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

採用された回答

dpb
dpb 2019 年 5 月 1 日
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...

その他の回答 (1 件)

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

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by