How to write this line of code to create a Matlab table?

I have unknown number of column variables in cell array:
Var{1} = [ 1; 2; 3; 4; 7];
Var{2} = [33; 10; 46; 7; 2];
...
If I knew how many variables I have, I would be able to manually form my table by:
T1 = table(Var{1}, Var{2}, ...);
The problem is that I do not know how many variables each time my program will generate. In this case, how could I write the code to create the table?
I tried the below, but it does not work:
T1 = table(Var);
Many thanks.

 Accepted Answer

cell2table and probably provide VariableNames parameter

10 Comments

Many thanks for pointing me towards that direction.
Below is my code. Unfortunately, it still does not work
TT = cell2table(Var);
File_W = 'test.xlsx';
writetable_units(TT, File_W);
Error using writetable_units (line 11)
The data block starting at cell 'A3' exceeds the sheet
boundaries by 0 row(s) and 1874492 column(s).
Error in A01_NC (line 68)
writetable_units(TT, File_W);
Here is what my Var look like:
Var =
1×69 cell array
Columns 1 through 3
{27404×1 double} {27404×12 char} {27404×12 char}
Columns 4 through 6
{27404×1 char} {27404×1 char} {27404×9 char}
I am not able to find any function named writetable_units. The error message suggests that whatever it is, it is not written in MATLAB m code so it might be difficult to find source code for.
The error message refers to 1874492 columns, which is far beyond the limit for xlsx files. However it is not obvious to me how it could have gotten that many columns out of your data, which is why I would need to look at the source code.
Though I do notice that you only happen to show the first 6 out of 69 cells. Is it possible that some of the cells contain numeric values with more than one column? For example 27204 x 20000 double would need 20000 excel columns which would be too much for xlsx format.
Thanks, Walter!
Please ignore the readtable_units function. It is a slightly modified version of the readtable. It is essentially the same as the readtable function. This error is unrelated to that function for sure.
My data has 69 columns, each column has 27404 rows. That's it. The 1874492 columns probably is a proof that the below command is not working as we hope it should.
TT = cell2table(Var);
What does size(TT) show? Also experiment with cell2table(Var(:))
Many thanks!
Size(TT) shows: 1 x 69.
I also tested TT=cell2table(Var(:)) and the size(TT) shows: 69 x 1.
vartypes = cellfun(@class, Var, 'uniform', 0);
nonchar = find(~ismember(vartypes, {'string', 'char'}));
varsizes = cellfun(@size, Var, 'uniform', 0);
vardims = cellfun(@ndims, Var);
oddsized = find(vardims ~= 2);
if ~isempty(oddsized)
fprintf('Some variables are multi-dimensional! Index: \n');
disp(oddsized)
fprintf('sizes:\n');
celldisp(vardims(oddsized))
end
nonchar = setdiff(nonchar, oddsized); %don't complain twice
nonchar_sizes = vertcat(varsizes{nonchar});
multicol = find(nonchar_sizes(:,2) > 1);
if ~isempty(multicol)
fprintf('Some non-char variables have multiple columns! Index: \n');
disp(nonchar(multicol));
fprintf('sizes:\n');
nonchar_sizes(multicol, :);
end
if isempty(oddsized) && isempty(multicol)
fprintf('All non-char variables are single column, as desired\n');
end
Oh wait:
TT = table(Var{:});
should do the trick.
Hooray!!!
Thank you so much. I really appreciate your weekend time to help me out. Enjoy the rest of your weekend.

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Tags

Asked:

on 5 Dec 2020

Commented:

on 5 Dec 2020

Community Treasure Hunt

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

Start Hunting!