Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Display Nested Cell Array as Tree-Structure

Subject: Display Nested Cell Array as Tree-Structure

From: Zeph Davies

Date: 9 May, 2012 12:57:07

Message: 1 of 3

For example, if i have an arbitrary cell array:

A(1).a = 1
A(1).b = 2
A(1).A.aa = 3
A(1).A.ab = 4
A(2).c = 5
A(2).d = 6
A(2).B(1).b1c = 7
A(2).B(1).b1d = 8
A(2).B(2).b2c = 9
A(2).B(2).b2d = 10

I'd like to display it like this:

PrintCell(A)

A 1:
a = 1
b = 2
A :
aa = 3
ab = 4
A 2:
c = 5
d = 6
B 1:
b1c = 7
b1d = 8
B 2:
b2c = 9
b2d = 10

Where the number of 'tabs' is the level in the cell array,
Where additional cell arrays get opened at they get reached.
Where each field can be unique (rendering structures useless).

So far i have the following, but it does not work correctly, because of the use of inputname(1):

function PrintCell(Cell,level)
  if nargin<2; level=0; end
  for i = 1:length(Cell)
    tabs = ''; for j = 1:level; tabs = [tabs sprintf(' ')]; end
    disp([tabs inputname(1) ' ' num2str(i) ':']);
    tabs = [tabs sprintf(' ')];
    Field = fieldnames(Cell{i});
    for j = 1:length(Field)
      Value = Cell{i}.(Field{j});
      if isnumeric(Value)||ischar(Value); disp([tabs Field{j} ': ' num2str(Value)])
      elseif iscell(Value); PrintCell(Value,level+1)
      else error('Unknown class');
      end
    end
  end
end

Subject: Display Nested Cell Array as Tree-Structure

From: Zeph Davies

Date: 9 May, 2012 13:03:09

Message: 2 of 3

Sorry, i mean:

A{1}.a = 1;
A{1}.b = 2;
A{1}.A{1}.aa = 3;
A{1}.A{1}.ab = 4;
A{2}.c = 5;
A{2}.d = 6;
A{2}.B{1}.b1c = 7;
A{2}.B{1}.b1d = 8;
A{2}.B{2}.b2c = 9;
A{2}.B{2}.b2d = 10;

Subject: Display Nested Cell Array as Tree-Structure

From: Zeph Davies

Date: 30 May, 2012 10:43:06

Message: 3 of 3

Did it.
Messy, but works.

function Main
  A{1}.a1a = 1;
  A{1}.a1b = 2;
  A{1}.A{1}.a1a1a = 3.0;
  A{1}.A{1}.a1a1b = '4';
  A{2}.a2a = 5;
  A{2}.a2b = 6;
  A{2}.B{1}.a2b1a = '7';
  A{2}.B{1}.a2b1b = 8E-3;
  A{2}.B{2}.C{1}.a2b2c1 = '9';
  A{2}.B{2}.C{2}.D{1}.a2b2c2d1 = 10.1;
  
  PrintCell(A)
end

function PrintCell(Cell)
  for i = 1:length(Cell)
    if iscell(Cell(i))
      fprintf('%s%i:\n',inputname(1),i)
      DigIntoCell(Cell(i),1)
    else
      error('Input must be a Cell Structure')
    end
  end
end

function DigIntoCell(Cell,level)
  for i = 1:length(Cell)
    tabs = ''; for j = 1:level; tabs = [tabs sprintf(' ')]; end
    Field = fieldnames(Cell{i});
    for j = 1:length(Field)
      Value = Cell{i}.(Field{j});
      if iscell(Value)
        for k = 1:length(Value)
          fprintf('%s%s%i:\n',tabs,Field{j},k)
          DigIntoCell(Value(k),level+1)
        end
      else
        fprintf('%s%s: %s\n',tabs,Field{j},num2str(Value))
      end
    end
  end
end

Outputs:
A1:
  a1a: 1
  a1b: 2
  A1:
    a1a1a: 3
    a1a1b: 4
A2:
  a2a: 5
  a2b: 6
  B1:
    a2b1a: 7
    a2b1b: 0.008
  B2:
    C1:
      a2b2c1: 9
    C2:
      D1:
        a2b2c2d1: 10.1

Sorry the 'tabs' don't show here.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us