Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to update a public property in a method of the class

Subject: How to update a public property in a method of the class

From: Leo Kay

Date: 11 Jul, 2014 16:03:10

Message: 1 of 6

Hi Folks,

I have a class object QG initialised as gr = QG(constructor).
It has a public property gr.spectrum which is initialised to [] at the start.
I dont know if this is necessary, but I create a set method for spectrum, and set gr.spectrum = A_Vector externally (that is the value is set using the method set.spectrum(argument)).

Then there is a method function of gr.doesSomething that does some stuff and within the method, according to an if statement, the method imperatively needs to update the property QG.spectrum within, and the updated property has to be accessed after the if statement by declarations in the method.

I check that the property QG.spectrum (as it is declared in the method making use of the property) has indeed updated in the method by printing out the new size of the property, and the latter always suggests it has. The trouble is that, if I then call gr.spectrum externally on the matlab workspace, it returns the non updated vector, and some of the other methods that rely on the updated vector do not seem to work because the sizes are not consitent. What is going on? Why is the property vector not really updated?

Help appreciated thanks

Subject: How to update a public property in a method of the class

From: Steven Lord

Date: 11 Jul, 2014 17:22:06

Message: 2 of 6


"Leo Kay" <anousleshommes@hotmail.com> wrote in message
news:lpp1ru$d4s$1@newscl01ah.mathworks.com...
> Hi Folks,
>
> I have a class object QG initialised as gr = QG(constructor).
> It has a public property gr.spectrum which is initialised to [] at the
> start.
> I dont know if this is necessary, but I create a set method for spectrum,
> and set gr.spectrum = A_Vector externally (that is the value is set using
> the method set.spectrum(argument)).
>
> Then there is a method function of gr.doesSomething that does some stuff
> and within the method, according to an if statement, the method
> imperatively needs to update the property QG.spectrum within, and the
> updated property has to be accessed after the if statement by declarations
> in the method.
>
> I check that the property QG.spectrum (as it is declared in the method
> making use of the property) has indeed updated in the method by printing
> out the new size of the property, and the latter always suggests it has.
> The trouble is that, if I then call gr.spectrum externally on the matlab
> workspace, it returns the non updated vector, and some of the other
> methods that rely on the updated vector do not seem to work because the
> sizes are not consitent. What is going on? Why is the property vector not
> really updated?
>
> Help appreciated thanks

Your class QG is a value class not a handle class. Changes made to instances
of value classes inside a function or method do NOT automatically update the
copy of that object outside the function or method; you would need to return
the modified object to do that.

http://www.mathworks.com/help/matlab/handle-classes.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to update a public property in a method of the class

From: Leo Kay

Date: 11 Jul, 2014 19:12:17

Message: 3 of 6

"Steven Lord" <Steven_Lord@mathworks.com> wrote in message <lpp6gk$plj$1@newscl01ah.mathworks.com>...
>
> "Leo Kay" <anousleshommes@hotmail.com> wrote in message
> news:lpp1ru$d4s$1@newscl01ah.mathworks.com...
> > Hi Folks,
> >
> > I have a class object QG initialised as gr = QG(constructor).
> > It has a public property gr.spectrum which is initialised to [] at the
> > start.
> > I dont know if this is necessary, but I create a set method for spectrum,
> > and set gr.spectrum = A_Vector externally (that is the value is set using
> > the method set.spectrum(argument)).
> >
> > Then there is a method function of gr.doesSomething that does some stuff
> > and within the method, according to an if statement, the method
> > imperatively needs to update the property QG.spectrum within, and the
> > updated property has to be accessed after the if statement by declarations
> > in the method.
> >
> > I check that the property QG.spectrum (as it is declared in the method
> > making use of the property) has indeed updated in the method by printing
> > out the new size of the property, and the latter always suggests it has.
> > The trouble is that, if I then call gr.spectrum externally on the matlab
> > workspace, it returns the non updated vector, and some of the other
> > methods that rely on the updated vector do not seem to work because the
> > sizes are not consitent. What is going on? Why is the property vector not
> > really updated?
> >
> > Help appreciated thanks
>
> Your class QG is a value class not a handle class. Changes made to instances
> of value classes inside a function or method do NOT automatically update the
> copy of that object outside the function or method; you would need to return
> the modified object to do that.
>
> http://www.mathworks.com/help/matlab/handle-classes.html
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Hi Steve. Would you mind explaining how I could go about creating such a property handle please? The documentation looks very dense and I don't have time to read through to be honest so a good tin is very welcome. How do I instantiate a handle property so that I can change it at will as described above (changing values in the method )?

Subject: How to update a public property in a method of the class

From: Leo Kay

Date: 12 Jul, 2014 02:51:13

Message: 4 of 6

Hi Steve
 Would you mind elaborating on my request above please? Thanks.

Subject: How to update a public property in a method of the class

From: Leo Kay

Date: 15 Jul, 2014 11:16:14

Message: 5 of 6

"Leo Kay" <anousleshommes@hotmail.com> wrote in message <lpq7r1$gf6$1@newscl01ah.mathworks.com>...
> Hi Steve
> Would you mind elaborating on my request above please? Thanks.

NO?

Subject: How to update a public property in a method of the class

From: Steven Lord

Date: 15 Jul, 2014 14:14:54

Message: 6 of 6


"Leo Kay" <anousleshommes@hotmail.com> wrote in message
news:lq32hu$ivh$1@newscl01ah.mathworks.com...
> "Leo Kay" <anousleshommes@hotmail.com> wrote in message
> <lpq7r1$gf6$1@newscl01ah.mathworks.com>...
>> Hi Steve
>> Would you mind elaborating on my request above please? Thanks.
>
> NO?

I was out of town for a long weekend and was not reading CSSM during that
time.


Your class can be either a handle class or a value class; you can't really
do the "hybrid" approach you asked about.

To do what you're trying to do, either:

1) Make your class a handle class by inheriting from the handle base class.
This will bring some changes in behavior, most notably:

a = instanceOfHandleObject;
b = a;
a.property1 = newValue

will change b's property1 value to newValue as well.

2) Call your method with the object both as input and output and return the
modified object from the method.

If your object is meant to represent a physical object (like a figure window
on the screen or a file on disk) then making it a handle (so all the copies
of the object refer to the same physical object) may be appropriate.

If on the other hand executing those lines of code above should NOT link a
and b's properties, I would recommend the second option.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us