assign names in order?

Hello, I really want to know if there is any function or tool that I can use to give diferents names to diferents variables. I'm studying any features (like area, values of pixel, ecc..) of somes regions in an image, and then I want to save these, and here I have the problem. if I woul to save, for example: image 1: region1: num of object=...; area=...; centroid:... region2: num of object=...; area=... image 2:..... How can I "name", or "appoint" diferents features or image in order that I study it? Because I want to save all in a file without overwrite... I don't know if I explain it very well, but thank you

 Accepted Answer

While you can generate variable names on the fly (using eval), it's not a good idea as it makes the code harder to understand, harder to debug, harder to check for syntax error and slower to execute.
In your case, what I would do is create structure arrays:
%hypothetical processing code
for imgindex = 1 : numimages
img = loadimage(imgindex); %load an image
numregions = regioncount(img); %get number of region
for regionindex = 1 : numregions
[area, centroid] = regionproperties(img, regionindex); %get property of region
%HERE is where you store the result in structure arrays:
imageprops(imgindex).region(regionindex).area = area;
imageprops(imgindex).region(regionindex).centroid = centroid;
end
end
To access the area of the 5th region of the 3rd image:
area = imageprops(3).region(5).area;

3 Comments

Maria
Maria on 21 Jan 2015
ok, thanks. At the end, I did it in this way (the way you say). I was asking because I thought that saving in different variables would be more efficient, faster to execute, ecc... But you said the opposite, so thank you!! I have another question that maybe you can help me. I have 2 differents functions (created by me) with small differences, how can I see which one is faster? or more efficient? Maybe you know how thank you!!
As of R2014, the simplest way to compare functions is to use timeit. If you require more help start a new question.
Maria
Maria on 22 Jan 2015
Thank you!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 Jan 2015

Commented:

on 22 Jan 2015

Community Treasure Hunt

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

Start Hunting!