i'm getting the following error when trying to create a table with multiple variables from if statements, any thoughts?

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

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.
BL=disp(' ')
and event_price/name are formatted the same way as the other if statements with each if statement running under a for loop
WIth lot of variables undefined it is tough for us to help you. Say Room, D and Location are defined....what are BL, event_name, event_price????Show us the complete code which made you to get this error.
here is a link to the doc with the code.
the code thats producing the errors is too long and spread through different sections to write out in here
i using it as a place as a place holder in the table i dont need a value for that section
why don't you attach .m file here? It would be much easier.

Sign in to comment.

Answers (2)

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.
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

Subnote: You will need r2017a or later to use " in the source code . r2016b introduced string objects but not the source code syntax .

Sign in to comment.

Categories

Asked:

on 19 Nov 2018

Commented:

on 20 Nov 2018

Community Treasure Hunt

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

Start Hunting!