Append a row to a workspace table

3 ビュー (過去 30 日間)
Jo Bremer Balestracci
Jo Bremer Balestracci 2020 年 9 月 29 日
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 件のコメント
Jo Bremer Balestracci
Jo Bremer Balestracci 2020 年 9 月 29 日
編集済み: Jo Bremer Balestracci 2020 年 9 月 29 日
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

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 9 月 29 日
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 件のコメント
Steven Lord
Steven Lord 2020 年 9 月 30 日
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 2020 年 10 月 1 日
Thank you. I will double check the quote and try it again using my longer string.

サインインしてコメントする。

その他の回答 (1 件)

Stijn Haenen
Stijn Haenen 2020 年 9 月 29 日
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 件のコメント
Jo Bremer Balestracci
Jo Bremer Balestracci 2020 年 9 月 29 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by