How to print contents of nested cell array to uitable?

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

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

Request you to Please read my question again. I have already tried 'Vertcat" just for your information.
If i use Vertcat, my answer would be A{1},A{2},A{3} , all printed in single column. How can i then again export them to a uitable with multiple columns in A{1},A{2} and A{3} ?
I would still get the whole data in a single coloumn, which i dont want. I want A{1} in first coloumn of uitable, A{2} in second column of uitable and so on..
Ok apologies I misread your question.
Thanks for your reply anyways. please let me know if you found a solution.
Sorry i did not check your edit. I will try this out and update you.
Turlough Hughes Thanks a lot, it works . But Another short question...
I have a cell array B 16x3, where first column might have only data in 3 rows, second would have data in 16 rows and so on... This cell array B is updated in a loop, So i have data in different number of rows in each column.
I would like to store this cell array B in a new cell array C with only first column of B, in another cell array D with only second column of B and so on.. But i could not find an exact way, instead ended up storing in a nested cell array.
What i did was ..
C{:,Count} = B(:,Count_1);
Count and Count_1 will be incremented in loops. So when Count and Count_1 is 1, it would store the first column of B in a nested cell in first row and first column of C.
Is there any way to avoid storing as a nested cell array?
I have a cell array B 16x3, where first column might have only data in 3 rows
What would be in the empty locations ?
Should C end up containing only the first 3 cells from B(:,1) if the first column has data only in 3 rows? You would want it to end up as a 3 x 1 cell array? Or you would want it to end up as a 16 x 1 cell array?
Could you confirm that each entry such as B{2,14} would itself be a vector or array, and not just a scalar?
@Walter Roberson The empty locations will just contain []. I wanted to end up in first iteration C as 3x1 and D as 16x1.
Once I finished creating arrays C,D .. I also have other array E 11x3cell whose first column I will move to 2nd Coloumn of C and second coloumn I will move to 2nd coloumn it D (just for your info).
Now C should change to 11x1 cell. This is what I am looking for.
@Suresh B, the curly braces in your second last comment mean that data on the right gets put into a cell. One good reason for using a cell array is when you have columns of data with different numbers of rows and you want all these columns in one variable. For example
C = {randi(100,[3,1]), randi(100,[16,1])}
It is also possible to do what you requested as follows:
C(:,Count) = B(~cellfun(@isempty,B(:,Count_1)),Count_1)
but you would need to resize C each time depending on the values on the right hand side, and this just seems like a bad idea.
If the contents of your cells are (fingers crossed) all scalars of class double you could simplify things greatly storing the values of a given column in a single cell un-nested, especially as the columns are changing size all the time. Are you intending on displaying C in the uitable?
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.
Can you attach the variables as a .mat file?

Sign in to comment.

More Answers (1)

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

Thansk for the reply :)
I did not get this part.. FirstN = @(V,N) V(1:N)
Also i got an error "Conversion from char to cell" is not possilble.
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.
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.

Categories

Products

Release

R2012b

Community Treasure Hunt

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

Start Hunting!