Gloria
Gloria

Reputation: 329

how to edit cells array in matlab

I have this cells array which is came from a mat lab code that generates dewey IDs:

 POT1 = 

        'a0'      []      []      []
        'a0'    'c0'      []      []
        'a0'    'b0'      []      []
        'a0'    'c1'      []      []
        'a0'    'd0'      []      []
        'a0'    'c0'    'd1'      []
        'a0'    'b0'    'd2'      []
        'a0'    'd0'    'd3'      []
        'a0'    'd0'    'c2'      []
        'a0'    'd0'    'b1'      []
        'a0'    'd0'    'd4'      []
        'a0'    'c1'    'c3'      []
        'a0'    'c1'    'b2'      []
        'a0'    'c1'    'c3'    'd5'
        'a0'    'c1'    'b2'    'd6'
        'a0'    'd0'    'b1'    'd7'
        'a0'    'd0'    'c2'    'd8'

note that column 1 is parent of column 2 and column 2 is paret of column 3..etc

so I want to build a code that gives the full name of each cell as follow:

POT1 =

  a0      []       []          []
  a0    a0.c0      []          []
  a0    a0.b0      []          []
  a0    a0.c1      []          []
  a0    a0.d0      []          []
  a0    a0.c0    a0.c0.d1      []
  a0    a0.b0    a0.b0.d2      []
  a0    a0.d0    a0.d0.d3      []
  a0    a0.d0    a0.d0.c2      []
  a0    a0.d0    a0.d0.b1      []
  .
  .
  .
  .     

The code which I build is not complete and gives me :" Index exceeds matrix dimensions" error :

for i=1:length(POT1)
      for j=3:size(POT1,2)
          if ~isempty(POT1{i,j})
          POT1{i,j}=[POT1{i,j-2} POT1{i,j-1} POT1{i,j}];
          end
      end
  end
  POT1

Upvotes: 3

Views: 1669

Answers (2)

Gunther Struyf
Gunther Struyf

Reputation: 11168

I think you're on the right track, but it's easier if you process it column by column. This way, you just have to look one column back for each entry:

for jj=2:size(POT1,2)
    for ii=1:size(POT1,1)
        if ~isempty(POT1{ii,jj})
            POT1{ii,jj}=[POT1{ii,jj-1} '.' POT1{ii,jj}];
        end
    end
end

btw: length returns the Length of vector or largest array dimension, so next time, better use size.

Upvotes: 4

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

If you have vectors with ' %Two spaces ' on the empty spaces it will be really easy.

You can just transform it into a matrix and the rest is simple as this:

[POT1(:,1:2) '.' POT1(:,3:4)] 

Afterwards you can just strip the spaces and done.

Upvotes: 2

Related Questions