Save data from cycle into one table

Hello, I have a code where the aotput is x and y, but I would like to store this data into one table or varieble. Problem is, that it gives me data only in one cyklus and it gives me it 15 times the same data, and than for h=2 it gives me 14 times the same value .. and so on
I would like to get one x and y from every loop and save it in one table or whatever, so after the code is ended, I would like to have everything in one place, if you understand. Now it gives me separately for every loop.
Thank you so much

 Accepted Answer

x and y are the locations of all the white pixels in the image. There will likely be tens of thousands of values in each vector. Did you want the x and y of the blob centroid(s) instead?
What if you just stored the x and y in a cell array
ca{h, 1} = x;
ca{h, 2} = y;
Not sure why you have a loop within a loop but maybe you want i instead of h in the lines of code above.
Can we run your code with the demo that ships with MATLAB, rhinos.avi, or do we need your specific video to be attached?

8 Comments

Well we can try it, because I do not know what to do next.
Yes I have two loops because I need to apply the segmentation and this was only way I can do it, because I do not have so much experience with matlab.
We can try it, but use the demo, it will work, but you do not see the result I need. But never mind, I only want to solve the problem with many datas in one cycle and how to save the data.
Thank you for helping me
As you can see, if I do for example regionprops(BW,"centroid")
it gives me this ... but like 15 times the same table ...
I don't know what you want. You just say you want the x and y coordinates of the segmented image. This is a classic XY Problem.
I told you how do get x and y for all white pixels in the segmented image. But since you came back I presume that is not what you wanted. You also got the centroids but I don't understand your pictures. Centroids are a 2 element vector, not a 5 by 1 vector. You have two structures, each with a field called Centroid but it has only one number in it. I don't know how you got those. What you should do is
props = regionprops(BW, 'Centroid');
xyCentroids = vertcat(props.Centroid)
So let's say that you are able to do whatever it is that you think you want (finding all x,y or just the x,y of the blob centroids, or whatever). Then what? What will you do with that information? Why do you even care about measuring it?
Also, please explain your thinking about why you have a whilte loop inside a for loop inside a while loop instead of a single for loop over the number of frames
for h = 1 : 2000 : vid.NumFrames
I think my Image Processing Tutorial in my File Exchange would help you:
You are right I change it, now I have while cycle and for cycle .. I also get the x and y values.
Now the problem is, that I getting this data according to image segmentation, So if the segmentation is not good, it gives me NaN ... and I can to save it into table, because there is no value.
OK, so you need to improve your segmentation algorithm if you think it's really there but your current algorithm is not finding it.
Thank you, when when I did it.
How can I store all this value from every cycle into one table. Because I will get many values but only for one cycle. I would like to store them together.
You can ask regionprops for a table. Then you can append the table for this frame to the growing master table.
thisTable = regionprops('table', BW);
if frameNumber == 1
masterTable = thisTable;
else
masterTable = [masterTable; thisTable];
end
Thank you so much, I appreciate it a lot
It helped

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!