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 commentaire

Jo Bremer Balestracci
Jo Bremer Balestracci le 29 Sep 2020
Modifié(e) : Jo Bremer Balestracci le 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

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 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 commentaires

Jo Bremer Balestracci
Jo Bremer Balestracci le 30 Sep 2020
Modifié(e) : Jo Bremer Balestracci le 30 Sep 2020
I have tried the fix and it works. But I have twenty (20) variables and it doesn't work. It does work if I limit my variables.
So this "variableNames = {'BookID', 'State', 'noofVols', 'Vol_Interval', 'VOL1', 'Vol2', 'Vol3'};" needed to be expanded to contain 20 variables. I did expanded it to 20 and that is when I got this error:
variableNames = {'BookID', 'State', 'noofVols', 'Vol_Interval', 'VOL1', 'Vol2', 'Vol3', 'Vol4', 'Vol5', 'Vol6', 'Vol7', 'Vol8', 'Vol9', 'Vol10', 'Vol11', 'Vol12' 'Vol13', 'Vol14', 'Vol15’, 'Vol16', 'Vol17', 'Vol18’ };
↑ - this symbol was under Vol16 Error: Unexpected MATLAB expression.
I then removed all of the Variables past Vol15 to see what would happen and I got this error:
variableNames = {'BookID', 'State', 'noofVols', 'Vol_Interval', 'VOL1', 'Vol2', 'Vol3', 'Vol4', 'Vol5', 'Vol6', 'Vol7', 'Vol8', 'Vol9', 'Vol10', 'Vol11', 'Vol12' 'Vol13', 'Vol14', 'Vol15’ }; ↑ -this symbol was now under Vol15
Error: Character vector is not terminated properly.
So I started to eliminate variable Vol15 and it work. This this means I had eighteen (18) variables. Is there a limitations on the number of Variables?
You're missing a comma
, 'Vol12' 'Vol13',
% ^
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 le 1 Oct 2020
Thank you. I will double check the quote and try it again using my longer string.

Connectez-vous pour commenter.

Plus de réponses (1)

Stijn Haenen
Stijn Haenen le 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 commentaire

Jo Bremer Balestracci
Jo Bremer Balestracci le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by