Vertcat tables with structure variables with different fieldnames
Hello! I’m having troubles when creating a table within a loop because some table variables are structure arrays that have different fieldnames. I get the following error :
Error using vertcat : Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
Example of my code :
%Inputs
ExampleTable = table();
Orig_1 = struct(‘m4ADG’,’folder_1′,’Group’,’folder_group’);
Orig_2 = struct(‘m5B0′,’folder_2′,’Group’,’folder_group’);
item = {‘charArray’,[1 1 1], Orig_1; ‘otherChar’,[1 2 1], Orig_2};
%function
VarNames = {‘Var1′,’Var2′,’Var3’};
for j = 1:size(item,1)
Var{1,1} = item{j,1};
Var{1,2} = item{j,2};
Var{1,3} = item{j,3};
NewTable = cell2table(Var);
NewTable.Properties.VariableNames = VarNames;
if isempty(ExampleTable)
ExampleTable = NewTable;
else
tmp = table2cell(NewTable);
ExampleTable = [ExampleTable; tmp];
end
clear NewTable Var
end
This whole thing runs within a class with the defined property ExampleTable as table and item, Orig_1|2 comes from elsewhere. Do you know how I can fix the problem?Hello! I’m having troubles when creating a table within a loop because some table variables are structure arrays that have different fieldnames. I get the following error :
Error using vertcat : Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
Example of my code :
%Inputs
ExampleTable = table();
Orig_1 = struct(‘m4ADG’,’folder_1′,’Group’,’folder_group’);
Orig_2 = struct(‘m5B0′,’folder_2′,’Group’,’folder_group’);
item = {‘charArray’,[1 1 1], Orig_1; ‘otherChar’,[1 2 1], Orig_2};
%function
VarNames = {‘Var1′,’Var2′,’Var3’};
for j = 1:size(item,1)
Var{1,1} = item{j,1};
Var{1,2} = item{j,2};
Var{1,3} = item{j,3};
NewTable = cell2table(Var);
NewTable.Properties.VariableNames = VarNames;
if isempty(ExampleTable)
ExampleTable = NewTable;
else
tmp = table2cell(NewTable);
ExampleTable = [ExampleTable; tmp];
end
clear NewTable Var
end
This whole thing runs within a class with the defined property ExampleTable as table and item, Orig_1|2 comes from elsewhere. Do you know how I can fix the problem? Hello! I’m having troubles when creating a table within a loop because some table variables are structure arrays that have different fieldnames. I get the following error :
Error using vertcat : Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
Example of my code :
%Inputs
ExampleTable = table();
Orig_1 = struct(‘m4ADG’,’folder_1′,’Group’,’folder_group’);
Orig_2 = struct(‘m5B0′,’folder_2′,’Group’,’folder_group’);
item = {‘charArray’,[1 1 1], Orig_1; ‘otherChar’,[1 2 1], Orig_2};
%function
VarNames = {‘Var1′,’Var2′,’Var3’};
for j = 1:size(item,1)
Var{1,1} = item{j,1};
Var{1,2} = item{j,2};
Var{1,3} = item{j,3};
NewTable = cell2table(Var);
NewTable.Properties.VariableNames = VarNames;
if isempty(ExampleTable)
ExampleTable = NewTable;
else
tmp = table2cell(NewTable);
ExampleTable = [ExampleTable; tmp];
end
clear NewTable Var
end
This whole thing runs within a class with the defined property ExampleTable as table and item, Orig_1|2 comes from elsewhere. Do you know how I can fix the problem? table, structures MATLAB Answers — New Questions