Append a row to a workspace table

2 Ansichten (letzte 30 Tage)
Jo Bremer Balestracci
Jo Bremer Balestracci am 29 Sep. 2020
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
  1 Kommentar
Jo Bremer Balestracci
Jo Bremer Balestracci am 29 Sep. 2020
Bearbeitet: Jo Bremer Balestracci am 29 Sep. 2020
I want to add a row at the end so that it looks like this:
BookID State noofVols Vol_Interval VOL1 Vol2 Vol3 Workpace would indicate
2 Open 2 4 BX2 not used not used 1x7
4 Closed 3 25 BX1 not used BX5 2 x 7
So everytime I run the code it adds a row to the "Configuration" table

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 29 Sep. 2020
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'}
  4 Kommentare
Steven Lord
Steven Lord am 30 Sep. 2020
The quote before Vol15 is the type of quote MATLAB accepts.
The quote after Vol15 is a "smart quote", meaning I suspect you wrote this code in Microsoft Outlook or in Microsoft Word. Replace the "smart quote" with a regular quote in the MATLAB Editor. It looks like your Vol18 have this same problem.
As for adding rows to a table:
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
patients(end, :) % Mr. Hayes
patients(end+1, :) = {'Branson', 'Male', 40, 68, 180, false, 123, 82};
patients(end-1:end, :) % Mr. Hayes and Mr. Branson
Jo Bremer Balestracci
Jo Bremer Balestracci am 1 Okt. 2020
Thank you. I will double check the quote and try it again using my longer string.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stijn Haenen
Stijn Haenen am 29 Sep. 2020
If you have two tables for example:
a=table();
a.Var1(1,1) = 1;
a.Var2(1,1) = 1;
a.Var1(2,1) = 3;
a.Var2(2,1) = 3;
b=table();
b.Var1(1,1) = 2;
b.Var2(1,1) = 2;
you can insert table b into table a with:
c=[a(1,:); b(1,:) ;a(2,:)];
  1 Kommentar
Jo Bremer Balestracci
Jo Bremer Balestracci am 29 Sep. 2020
If just using numbers, this works but I have a mixture of numbers and text. So when I subsitute Var2(1,1) = 'Open' I get an error. The error is:
Error using Possible_Fix.m (line 3) - Unable to perform assignment because the size of teh left side is 1-by-1 and the size of the right side is 1b-by-7.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by