How to print contents of nested cell array to uitable?

7 views (last 30 days)
I have a nested cell array A of 1x3cell and which further contains cell arrays of different dimensions like 2x1, 18x1, 15x1 and so on.. What i would like to do is display this nested cell array data into an uitable with first coloumn of uitable containing first element of A, second column containing second element of A and so on...
I have tried first of all, trying to un-nest the cell array A, using
horzcat(A{:});
so i could then use it for 'Data' of uitable , but it throws "Dimensions are inconsistent" ofcourse.
Also i have tried following:
table = uitable('Parent', gcf,'Data',A{:,:});
or
table = uitable('Parent', gcf,'Data',A{:,:}{:,:});
But unfortunately, none of them works out.. any help would be appreciated.

Accepted Answer

Turlough Hughes
Turlough Hughes on 15 Feb 2020
Edited: Turlough Hughes on 15 Feb 2020
I think the best thing to do is make another cell array, B, as follows:
nRows = max(cellfun(@numel,A))
nCols = numel(A)
B = cell(nRows,nCols);
for c = 1:nCols
B(:,c) = vertcat(A{1,c},cell(nRows - numel(A{1,c}),1));
end
This array can then be put into the uitable.
fig = uifigure;
uit = uitable(fig,'Data',B);
EDIT: I had indeed misread the question.
  10 Comments
ML_Analyst
ML_Analyst on 16 Feb 2020
Edited: ML_Analyst on 16 Feb 2020
yes i am intending to do that.. in a loop B gets calculated and it will actually have 4 columns of varying number of rows . So before the next loop begins.. i would like to store them in seperate cell arrays like D,F,H,J... (for simplicity i am giving you names in D,F.. in alphabets), so that i could later print the cell arrays D,F,.. to a uitable.
For the first loop I would like to store 1st column of B in 1st column of D, 2nd in 1st column of F, 3rd in 1st column of H, 4th in 1st column of J. For the second loop of B , i would like to store 1st column of B in 2nd coloumn of D, 2nd in 2nd column of F, 3rd in 2nd column of H, 4th in 2nd column of J... and third loop and so on.....
For the Same reason you had mentioned i chose the cell array because my array B generarated in a loop could have varying number of rows and i would like the data in one variable.
And the contents of my cell B which is generated are all "double" only numerical values.. no strings or anyother.
Turlough Hughes
Turlough Hughes on 16 Feb 2020
Edited: Turlough Hughes on 16 Feb 2020
Can you attach the variables as a .mat file?

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Feb 2020
Astr = cellfun(@string, A, 'uniform', 0);
Alen = cellfun(@numel, Astr);
maxlen = max(Alen);
FirstN = @(V,N) V(1:N);
Astr = cellfun(@(S) FirstN([S;strings(maxlen,1)], maxlen), Astr, 'uniform', 0);
Astr will now be a cell array of string arrays, with each of the string arrays being the same size. The initial part of each string array is the string() conversion of the cell contents; the rest is padding with empty strings out to the maximum length. You can now use Astr for the Data property of the uitable; at worst cellfun(@cellstr,Astr,'uniform',0) in order to get it into cell array of cell array of character vectors.
You might notice that everything has been converted to string (or character if you went for cellstr) . That has consequences for editing.
The reason for converting everything to string is that the Data property does not permit columns of unequal length, so we have to pad the columns out to equal length without accidentally introducing new data. Adding new blank entries is about the only socially accepted way of doing that, but blank entries requires that the entries be string or character vectors.
  3 Comments
Walter Roberson
Walter Roberson on 16 Feb 2020
FirstN = @(V,N) V(1:N)
Here I define an anonymous function that takes two variables, the first one is a vector and the second one is a length. The anonymous function takes the first so-many elements of the vector. For example if I let N be 8 and I have a cell array in which one entry be 8 entries and another be 11 entries, then when applied to the first entry it would return all of it, and when applied to the second entry it would return the first 8 out of 11 -- so you would end up with both entries being length 8.
Using functions like this are handy because it can save you from having to write a loop to chop everything to the same size.
I just noticed that your MATLAB release is R2012a. The code I posted will not work for you, as the string() datatype was not added until R2016b. It would be necessary to convert all of your data to cell array of character vectors, which is a bit harder to automate unless you already know what the datatype is of each cell.
ML_Analyst
ML_Analyst on 16 Feb 2020
The datatype of each cell would be always double .. no other data-types. If this is what you are looking for.

Sign in to comment.

Products


Release

R2012b

Community Treasure Hunt

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

Start Hunting!