In MATLAB, you can combine data in various ways, depending on the type of data you are working with, such as arrays, tables, structures, or specific object types.
MATLAB provides functions and operators to concatenate, merge, or append data, creating a larger dataset from smaller ones.
Common Data Combination Methods
The most frequent ways to combine data involve arrays and tables, which are fundamental MATLAB data types.
Combining Arrays
Arrays (including vectors and matrices) can be combined using square brackets []
for concatenation.
- Horizontal Concatenation: Appending arrays side-by-side.
A = [1 2 3]; B = [4 5 6]; C = [A B]; % C is [1 2 3 4 5 6]
- Vertical Concatenation: Stacking arrays on top of each other. The arrays must have the same number of columns.
A = [1 2; 3 4]; B = [5 6; 7 8]; C = [A; B]; % C is [1 2; 3 4; 5 6; 7 8]
Combining Tables
Tables are excellent for storing mixed-type data with variable names. You can combine them vertically or horizontally.
- Vertical Concatenation: Stacking tables with the same variables using
vertcat
.T1 = table([1; 2], {'A'; 'B'}, 'VariableNames', {'ID', 'Label'}); T2 = table([3; 4], {'C'; 'D'}, 'VariableNames', {'ID', 'Label'}); Tcombined = vertcat(T1, T2); % Tcombined looks like: % ID Label % __ _____ % 1 'A' % 2 'B' % 3 'C' % 4 'D'
- Horizontal Concatenation: Appending tables side-by-side with different variables using
horzcat
.T1 = table([1; 2], 'VariableNames', {'ID'}); T2 = table([10; 20], 'VariableNames', {'Value'}); Tcombined = horzcat(T1, T2); % Tcombined looks like: % ID Value % __ _____ % 1 10 % 2 20
- Joining Tables: Combining tables based on common key variables using
join
. This is similar to database joins.T1 = table([1; 2], {'A'; 'B'}, 'VariableNames', {'ID', 'Label'}); T2 = table([2; 1], [10; 20], 'VariableNames', {'ID', 'Value'}); Tjoined = join(T1, T2, 'Keys', 'ID'); % Tjoined looks like: % ID Label Value % __ _____ _____ % 1 'A' 20 % 2 'B' 10
Combining Specific Object Types: The combine
Function
For certain specialized data objects in toolboxes, dedicated functions exist for combining data. As highlighted in the reference:
combinedData = combine( data1 , data2 )
combines sequence data from two objects of the same class ( BioRead or BioMap ) and returns the data combinedData in a new object. The combine function concatenates the properties of the two objects.
This combine
function is specifically designed for bioinformatics objects (BioRead
and BioMap
) and concatenates their internal properties to merge sequence data from two sources into a single object instance. This is distinct from the general array or table concatenation methods and is tailored to the structure and content of these specific classes.
Summary Table
Data Type | Common Methods | Description |
---|---|---|
Arrays | [] (Horizontal/Vertical Concatenation) |
Simple concatenation along rows or columns. |
Tables | vertcat , horzcat , join |
Stacking, side-by-side appending, or merging based on key variables. |
Bioinformatics Objects (BioRead, BioMap) | combine(data1, data2) |
Specific function to concatenate object properties for these data types. |
Structures | Array concatenation [struct1 struct2] |
Creates an array of structures. Content merging is typically manual or via functions. |
Understanding your data type is the first step in determining the appropriate method for combining data in MATLAB.