How to print contents of nested cell array to uitable?
8 ビュー (過去 30 日間)
古いコメントを表示
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.
採用された回答
Turlough Hughes
2020 年 2 月 15 日
編集済み: Turlough Hughes
2020 年 2 月 15 日
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 件のコメント
Turlough Hughes
2020 年 2 月 16 日
編集済み: Turlough Hughes
2020 年 2 月 16 日
Can you attach the variables as a .mat file?
その他の回答 (1 件)
Walter Roberson
2020 年 2 月 16 日
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 件のコメント
Walter Roberson
2020 年 2 月 16 日
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.
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!