Cant join 2 tables with same headers. vertcat error

14 views (last 30 days)
Can someone explain why im getting this error? Im trying to join the rows of 2 tables who have the exact same headers, but im getting an error? Online says that vertcat or [T1;T2] should do the trick no problem, but this is not the case for me. Please help
a =
struct with fields:
id: 'a'
mode: 'read'
name: 'firstname'
type: 'double'
>> b = a
b =
struct with fields:
id: 'a'
mode: 'read'
name: 'firstname'
type: 'double'
>> b.name = 'differentfirstname'
b =
struct with fields:
id: 'a'
mode: 'read'
name: 'differentfirstname'
type: 'double'
>> Ta = struct2table(a)
>> Tb = struct2table(b)
>> [Ta;Tb]
An error occurred when concatenating the table variable 'name'
using VERTCAT.
Caused by:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Answers (1)

Adam Danz
Adam Danz on 26 Jan 2021
Edited: Adam Danz on 28 Jan 2021
The error is because of a difference in character length of the two "name" fields. Vertical concatenation of characters into a character array requires the same number of characters, even in a table.
Use a cell array of characters, a string (shown below), or a category instead.
a = struct('id','a','mode','read','name',"firstname",'type','double');
% string ^ ^
b = a;
b.name = "differentfirstname"; % string, not char-vector
Ta = struct2table(a);
Tb = struct2table(b);
[Ta;Tb]
ans = 2x4 table
id mode name type __ ____ ____________________ ______ a read "firstname" double a read "differentfirstname" double

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!