set object property of a user-defined class

Hello,
I want to set the properties "nb_pixel_horiz" and "nb_pixel_verti" of an object "p" that has the user-defined class "Picture".
I wrote
classdef Picture
properties
nb_pixel_horiz;
nb_pixel_verti;
end
methods
end
end
And I want to write in the main command something like:
p = Picture;
set(p,'nb_pixel_horiz', 2000);
set(p,'nb_pixel_verti', 1000);
But it does not work. Could someone help me?
Thanks! Guillaume

4 Comments

Just for my own interest since I use classes a lot, but have never aimed to get or set properties in that way...
Is there any particular reason you specifically wish to set properties like that rather than the more usual method of?:
p.nb_pixel_horiz = 2000;
utilising the set( obj, nb_pixel_horiz ) function if you have created one.
Creating your own set methods for the standard syntax is nice because it also allows you to validate inputs. The most common function I use nowadays is validateattributes so I can ensure the inputs to my class functions are correct and don't have to worry about bugs later on in the code.
Guillaume
Guillaume on 3 Sep 2014
Edited: Guillaume on 3 Sep 2014
I've never had a need to do this either, but I'll point out that a class that derives from hgetset automatically picks up your setter and getter functions, so you can do the same kind of validation with this syntax as well.
- The other Guillaume -
@ Adam: Yes you're right. Actually I am not an expert at all in classes. I don't know when and how to use them properly. That's why sometimes I am making things complicated when I could make them easy...
In case you have not yet come across it, the following document provides an excellent reference for helping to understand programming with classes in Matlab. I still refer back to it on many occasions:

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 3 Sep 2014
Edited: Guillaume on 3 Sep 2014
Hey, Another Guillaume!
You need to derive from hgsetget, so:
classdef Picture < hgsetget
properties
nb_pixel_horiz;
nb_pixel_verti;
end
end

3 Comments

Ok thank you other Guillaume. It's working now :-)
I have got another question.
I have 10 images and I would like to compute the standard deviation of the intensity values pixel by pixel. As a result, I have a matrix of standard deviations with the size (nb_pixel_horiz,nb_pixel_verti).
What is the best way to do this?
I tried to add the property "intensity_values" to the class "Picture" and then to write the following command:
I = get(p(1:10),'intensity_values');
But I get a cell with size 10x1 and I can't apply the std function on it.
Is there a solution or is my way of proceeding too complicated?
Thanks again! Guillaume
It's another question entirely, you should have started a new question really.
One way of doing what you want, assuming all pictures have the same size:
I = get(p(1:10),'intensity_values');
I = reshape(cell2mat(I), size(I{1}, 1), size(I{1}, 2), []); %transform into 3d matrix
pixstd = std(I, 0, 3);
OK thank you. Next time I will open a new question as you suggest.

Sign in to comment.

More Answers (1)

Unless you really need handle semantics, it would be enough to do
p.nb_pixel_horiz=2000
p.nb_pixel_verti=1000

Categories

Asked:

on 3 Sep 2014

Commented:

on 5 Sep 2014

Community Treasure Hunt

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

Start Hunting!