How to define an empty table with variable type of a column as a matrix?

Hello everyone,
I want to create an empty table with variable type of a column as a matrix.
I tried
tab = table('Size',[10,2],'VariableTypes',["double","double"]);
tab{1,1} = zeros(1,50); % I want to do this
tab{1,2} = zeros(1,25); % I want to do this
% Error : The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 1.
I understant this error however, I would like to know how can it be solved.
Thanks

2 Comments

You specified the table size as [10,2]. tab{1,1} and tab{1,2} is already a scalar whose value is 0.
You can't assign it to be a vector of 50 zeros or 25 zeros.
As I already mentioned, I understand the problem.
Thanks anyway.

Sign in to comment.

 Accepted Answer

I doubt that has a solution, with the way you created ‘tab’ originally, since it exceeds the column size you set for it.
You can do something like this, however that is a good as it gets:
tab = table('Size',[50,2],'VariableTypes',["double","double"]);
tab{1:50,1} = rand(50,1);
tab{1:25,2} = rand(25,1);
.

6 Comments

Thanks for the suggestion. However, I would like my table to look like the output of the following code
tab = table(zeros(10,20),zeros(10,30))
Then do exactly that!
When I ran that just now, it created a table with 2 varialbes, each with 10 rows of (1x20) or (1x30) vectors.
Note that this is different than the Question you originally posted.
This is one of the options. However, I have another contraint. The number of columns will vary for 1 to 16 in my application and I don't want to create a swtich case statement with 16 cases to initilize the table.
Using a table object from the outset may not be appropriate, then. Use a cell array or matrix, and then convert it to a table later. Tables definitely have their advantages, however they may not always be the best option in a particular situation.
Yes, this seems to be the only option I have left based on my requirements. Thanks a lot.

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!