How to append different tables of different class types (a table of class type datetime and a table of classs type double)
Show older comments
I have a table 'A' of size 5 x 1, class type 'datetime' and another table 'B' of size 5 x 1 and class type 'double'. I want to append both A and B so that a new table is 'C' is formed and size of table 'C' is 5 x 6 and the first column is datetime and remaining columns are double.
Ex:
A =
01/02/2016
01/03/2016
01/04/2016
01/05/2016
01/08/2016
B =
0.99 0.99 0.99 0.99 0.99
1.00 1.00 1.00 1.00 0.99
0.99 1.00 0.99 0.99 0.99
0.99 0.99 0.99 0.99 1.00
1.00 1.00 1.00 1.00 1.00
C is a 5 x 6 table of which first column is A and remaining columns are formed by appending B with A.
Thanks in advance..
Answers (1)
Peter Perkins
on 6 Feb 2017
I think you mean that B is 5x5, not 5x1.
You probably want to be careful using the word "table", since table is a data type in MATLAB. In fact, it's likely the data type you want. Depending on what you want, you might do one of two things.
1) Create a table with two variables, one of which is a column vector, one of which itself has 5 columns:
>> A = datetime({'01/02/2016';'01/03/2016';'01/04/2016';'01/05/2016';'01/08/2016'});
Warning: Successfully read the date/time text using the format 'MM/dd/uuuu', but their format is ambiguous and could also be
'dd/MM/uuuu'. Specify a format character vector to avoid ambiguity.
> In guessFormat (line 66)
In datetime (line 609)
>> B = [0.99 0.99 0.99 0.99 0.99;
1.00 1.00 1.00 1.00 0.99;
0.99 1.00 0.99 0.99 0.99;
0.99 0.99 0.99 0.99 1.00;
1.00 1.00 1.00 1.00 1.00];
>> T = table(A,B)
T =
5×2 table
A B
___________ ____________
02-Jan-2016 [1x5 double]
03-Jan-2016 [1x5 double]
04-Jan-2016 [1x5 double]
05-Jan-2016 [1x5 double]
08-Jan-2016 [1x5 double]
2) Split B into 5 separate variables in the table:
>> T = [table(A) array2table(B)]
T =
5×6 table
A B1 B2 B3 B4 B5
___________ ____ ____ ____ ____ ____
02-Jan-2016 0.99 0.99 0.99 0.99 0.99
03-Jan-2016 1 1 1 1 0.99
04-Jan-2016 0.99 1 0.99 0.99 0.99
05-Jan-2016 0.99 0.99 0.99 0.99 1
08-Jan-2016 1 1 1 1 1
Categories
Find more on Tables 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!