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

3 vues (au cours des 30 derniers jours)
Guillaume Migeon
Guillaume Migeon le 12 Avr 2019
Commenté : A. Sawas le 16 Avr 2019
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!

Réponse acceptée

A. Sawas
A. Sawas le 13 Avr 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
  2 commentaires
Guillaume Migeon
Guillaume Migeon le 16 Avr 2019
It helped me a lot, thank you very much!
A. Sawas
A. Sawas le 16 Avr 2019
My pleasure :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by