Effacer les filtres
Effacer les filtres

Cant join 2 tables with same headers. vertcat error

15 vues (au cours des 30 derniers jours)
Sergio Huerta
Sergio Huerta le 25 Jan 2021
Modifié(e) : Adam Danz le 28 Jan 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.

Réponses (1)

Adam Danz
Adam Danz le 26 Jan 2021
Modifié(e) : Adam Danz le 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

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by