Borrar filtros
Borrar filtros

Cant join 2 tables with same headers. vertcat error

11 visualizaciones (últimos 30 días)
Sergio Huerta
Sergio Huerta el 25 de En. de 2021
Editada: Adam Danz el 28 de En. de 2021
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.

Respuestas (1)

Adam Danz
Adam Danz el 26 de En. de 2021
Editada: Adam Danz el 28 de En. de 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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by