i'm getting the following error when trying to create a table with multiple variables from if statements, any thoughts?
Show older comments
T=('Booking')
T1=('Cost')
if Room==1
RT='Queen';
C=Q;
elseif Room==2
RT='King';
C=K;
elseif Room==3
RT='Double';
C=D;
end
disp(RT);
disp(C);
if Location==1
LC='Orlando';
elseif Location==2
LC='Hawaii';
elseif Location==3
LC='Los Angles';
elseif Location==4
LC='New York';
elseif Location==5
LC='DC';
end
LC1=LC
RT1=RT
T2=table(T,Location,BL,RT,C,event_name,event_price)
Error using tabular/verifyCountVars (line 339)
All variables must have the same number of rows.
Error in table (line 276)
numRows = tabular.verifyCountVars(vars);
Error in IDFK (line 1525)
T2=table(T,Location,BL,RT,C,event_name,event_price)
>>
13 Comments
madhan ravi
on 19 Nov 2018
Room?
Daniel Frantz
on 19 Nov 2018
Walter Roberson
on 19 Nov 2018
We are not given enough information to know the number of rows in BL or event_name or event_price
As you would seldom want to create a table with only one row, I suspect that you are looping over something but not indexing when you make the assignments, so you are only ending up with some variables being a single row instead of one row per input value.
Daniel Frantz
on 19 Nov 2018
Daniel Frantz
on 19 Nov 2018
Daniel Frantz
on 19 Nov 2018
Walter Roberson
on 19 Nov 2018
That document is not set up for public access.
Walter Roberson
on 19 Nov 2018
BL=disp(' ')
?? disp() does not return a value.
Daniel Frantz
on 19 Nov 2018
Daniel Frantz
on 19 Nov 2018
Luna
on 19 Nov 2018
why don't you attach .m file here? It would be much easier.
KSSV
on 19 Nov 2018
Jeez.....seems very big code.....:|
Answers (2)
Walter Roberson
on 19 Nov 2018
T2=cell2table({T,T1,LC,BL,RT,C,event_name,event_price}, 'VariableNames', {'T','T1','LC','BL','RT','C','event_name','event_price'});
Note:
In numerous places you are not validating your input. You do not, for example, check that the rows and columns are in range. You do not check that a row and column has not already been used. When generating randomly, you do not check that the random location has already been used. If the user requests more than 8 people and you fill up a helicopter tour then you do not start onto a second helicopter and you do not detect that you have run out of room. When prompting for character input, you require an exact case match -- if the user answers mt instead of MT for example then your code will crash.
Peter Perkins
on 19 Nov 2018
0 votes
Orthogonal to the advice that others have given:
If the table you are creating will eventually have more than one row (I assume it will), then creating LC and RT as char row vectors is not what you want. Prior to R2016b, Create them as cell arrays of char rows, i.e.
LC = {'Orlando'};
R2016b and later, use strings, i.e.
LC = "Orlando";
The reason is that when you go to add rows to a table with that contains a char row variable, all other rows for that var must be the same number of chars.
Actually, better advice would be to make RT and LC categorical variables. You will be happier in the long run.
2 Comments
Walter Roberson
on 19 Nov 2018
Subnote: You will need r2017a or later to use " in the source code . r2016b introduced string objects but not the source code syntax .
Peter Perkins
on 20 Nov 2018
Thanks, Walter, you are correct.
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!