matlabmatlab-app-designer

All tables in the bracketed expression must have the same variable names


I have two editable numeric fields and a table in app designer; the user can enter the values in these editable fields and then push a button. Then, the values are added to a table. Also, I provide an option to attach an excel folder that should have two columns to reflect on the table. Both of these work perfectly fine individually, but, if I added the values manually then attached an excel folder or vice versa, I get the following error: All tables in the bracketed expression must have the same variable names. The function that handles the editable fields:

  app.t = app.UITable.Data;
            x = app.xvalueEditField.Value;
            y = app.yvalueEditField.Value;
            nr = table(x, y);
            app.UITable.Data = [app.t; nr];  %% error happens here if I attach excel then add manually
            app.t = app.UITable.Data; 

The Function of the excel folder:

text = readtable([pathname filename], "Sheet",1, 'ReadVariableNames',false);
       fl = cellfun(@isnumeric,table2cell(text(1,:))); 
if (numel(fl(fl == false)) > 0) 
    flag = false;
else
    flag = true;
end
if (flag)
            A = [app.t; text];  %% error happens here if I add manually then attach
            app.UITable.Data = A;
            app.t = text; 
end

Note: these are only the parts of the function, where I attempt to combine values

Can someone please help me?

Thank you


Solution

  • The error message is telling you that table only allows you to vertically concatenate tables when the 'VariableNames' properties match. This is documented here: https://www.mathworks.com/help/matlab/ref/vertcat.html#btxzag0-1 .

    In your first code example, the table nr will have variable names x and y (derived from the names of the underlying variables you used to construct the table). You could fix that case by doing:

    % force nr to have the same VariableNames as app.t:
    nr = table(x, y, 'VariableNames', app.t.Properties.VariableNames);
    

    and in the second case, you can force text to have the correct variable names like this:

    text.Properties.VariableNames = app.t.Properties.VariableNames