set object property of a user-defined class
Show older comments
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.
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 -
Guillaume
on 5 Sep 2014
Adam
on 5 Sep 2014
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:
Accepted Answer
More Answers (1)
Matt J
on 3 Sep 2014
Unless you really need handle semantics, it would be enough to do
p.nb_pixel_horiz=2000
p.nb_pixel_verti=1000
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!