MATLAB: Append a row to a workspace table

appendrowvarsMATLAB

I imported a table made in excel into MATLAB and it is now a table in my workspace. The Name is "Configuration" and it is a 1 x 20 table in my workspace. If you open up "Configuration" it looks like this:
>>
1 2 3 4 5 6 7
BookID State noofVols Vol_Interval VOL1 Vol2 Vol3
______ _______ ________ ____________ _______ _______ _______
NaN NaN NaN NaN NaN NaN NaN
>> disp(Configuration)
T2 = Configuration
W=width(T2)
BookID = 2;
State = ‘Open’;
noofVols = 2;
Vol_Interval = 4;
VOL1 = ‘BX2’;
Vol2 = “not used’;
Vol3 = ‘not used’;
appendRowVars = {BookID, State, noofVols, Vol_Interval, VOL1, Vol2, Vol3}
T2(end+1) = appendRowVars;
I am using MATLAB version 9.5.0.944444 (R2018b),. When I run this I get T2 = 1 x 7, W = 7, then I get an error message "Conversion to double frm cell is not possible"
Help

Best Answer

Try this:
% Create initial table, which poster forgot to supply us with:
variableNames = {'BookID', 'State', 'noofVols', 'Vol_Interval', 'VOL1', 'Vol2', 'Vol3'};
numRows = 20;
nanColumn = nan(numRows, 1)
ca = {'Not Used'};
nanStr = repmat(ca, numRows, 1);
Configuration = table(nanColumn, nanStr, nanColumn, nanColumn, nanStr, nanStr, nanStr, 'VariableNames', variableNames)
% Get new varaible values somehow.
BookID = 2;
State = 'Open';
noofVols = 2;
Vol_Interval = 4;
VOL1 = 'BX2';
Vol2 = 'not used';
Vol3 = 'not used';
% Make a table of a single row, from the new variable vaues,
% that we can append to the Configuration table.
oneRowTable = table(BookID, {State}, noofVols, Vol_Interval, {VOL1}, {Vol2}, {Vol3}, 'VariableNames', variableNames)
% Now we have the table and we can now append a new row.
T2 = [Configuration; oneRowTable]
You'll see
T2 =
21×7 table
BookID State noofVols Vol_Interval VOL1 Vol2 Vol3
______ ____________ ________ ____________ ____________ ____________ ____________
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
NaN {'Not Used'} NaN NaN {'Not Used'} {'Not Used'} {'Not Used'}
2 {'Open' } 2 4 {'BX2' } {'not used'} {'not used'}