tags:

views:

27

answers:

2

Hi there,

I would like to know how can I get certain inputs and put them in more than one row in the cell array ... I basically want an array that updates one input per row in ever loop. The loop is looped 30 times, so I want to have 30 rows and 2 columns ( x and y columns)

I have this code :

For N=1:30
    .
    .
    .
    Binary = bwlabel(blacknwhite);
    s = regionprops(Binary,'centroid');
    centroids = cat(1, s.Centroid);
    hold(imgca,'on')
    plot(imgca,centroids(1,1), centroids(1,2),'r*')
    .
    .
    .
    end

I dont think this does what I want ... only the first row is updated in my loop .. So how can I create this cell array ?

If you want more info please tell me and I will update it right away.

Thanks !

+1  A: 

It's late so this is not a complete answer:

What are you trying to do ? Are you trying to build an Nx2 array one row at a time ? If you are, then you should:

  1. Pre-allocate the space for the whole array, with a statement such as this: newArray = zeros(N,2).
  2. Inside your loop write a statement such as newArray(N,:) = newValues, where newValues is a 1x2 array. It's very odd, to me, that your code snippet does not make use of the array index N.

Are you trying to build a cell array or an array ?

High Performance Mark
Im not sure if its a cell array or an array .. it looks like excel basically. At the end of the loop I get only 1 row and two columns worth of data ... I expect to get 23 to polyfit correctly.
ZaZu
+1  A: 

I assume that you want to store the centroids. In this case, you should use centroids(N,:)=cat.... Also, as @High Performance Mark says, you should preallocate the array.

centroids = zeros(30,2); %# this assumes 1 centroid per image. 
For N=1:30
    .
    .
    .
    Binary = bwlabel(blacknwhite);
    s = regionprops(Binary,'centroid');
    centroids(N,:) = cat(1, s.Centroid);
    hold(imgca,'on')
    plot(imgca,centroids(N,1), centroids(N,2),'r*')
    .
    .
    .
end
Jonas
Yes thank you for this, im still testing it out since the final code doesnt work yet.
ZaZu