Populate a table (or concatenate a cell array) with various data types

1 view (last 30 days)
Hi there!
I am stuck with a problem. I would like to display various kind of data into a table in the App Designer. I know I have to put all the data in a cell-array, but I always have multiple errors and I can't do properly the conversions.
For instance, let's say
A = {} % The cell array that contains the data of my table
B = [1 2 3;
4 5 6]
C = {'blabla', 5;
B , struct}
My goal at the end is to obtain
A = {'B' , '' , '' ; % Name of B, I know I can get it by a little function using inputName()
1 , 2 , 3 ;
4 , 5 , 6 ;
'' , '' , '' ; % A blank row for readability
'C' , '' , '' ;
'blabla' , 5 , '' ;
'2x3 double' , '1x1 struct' , '' } % We cannot print these kind of value in one single table cell, so we just indicate their class and size
I suppose I have to use functions such as reshape(), num2cell(), and use A = [A ; {...}] to concatenate my data, and this one induces more problems such as: how to resize in order to match dimensions...
Thank you very much for your help!

Accepted Answer

A. Sawas
A. Sawas on 13 Apr 2019
Try this function which will prety much do the job for you. You may optimize it to preallocate A as well.
function A = cell_format(varargin)
N = length(varargin);
m = max(cellfun(@(x)size(x,2),varargin));
A = cell(0,m);
for i=1:N
v = varargin{i};
[r,c] = size(v);
B = cell(r+1, m);
B{1,1} = inputname(i);
B(1,2:end) = {''};
if isnumeric(v)
B(2:end,1:c) = num2cell(v);
elseif iscell(v)
B(2:end,1:c) = v;
end
if(i>1)
A(end+1,:) = {''};
end
A = [A;B];
end
end
You will call it like this:
cell_format(B,C); % put the list of your variables in the function arguments

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!