From: Eric He on
Hi,

I am trying to build an object, with functions that use it and alter its parameter permanently, but am not sure how to change the scope of the parameters. The code for the object is as follows:

classdef exclass
properties
value
end
methods
function obj=exclass(in)
obj.value=in;
end
end
end

and the function that I'm using goes as follows:

function doubler(obj)
obj.value=obj.value*2;
fprintf('%d\n',obj.value());
end

How do I make it so that the function doubler can permanently change an object of exclass' value property?

Any help would be very appreciated.

Thanks,

Eric
From: Steven Lord on

"Eric He" <ericjhe(a)hotmail.com> wrote in message
news:hu8k30$7ph$1(a)fred.mathworks.com...
> Hi,
>
> I am trying to build an object, with functions that use it and alter its
> parameter permanently, but am not sure how to change the scope of the
> parameters. The code for the object is as follows:
>
> classdef exclass

Since this class does NOT inherit from handle, it is a value class and has
value semantics (like regular MATLAB arrays.)

> properties
> value
> end
> methods
> function obj=exclass(in)
> obj.value=in;
> end
> end
> end
>
> and the function that I'm using goes as follows:
>
> function doubler(obj)
> obj.value=obj.value*2;

This makes a change to the value property of the COPY of the object in the
doubler method workspace. Since you don't return the copy from the method,
the changes are discarded along with the method workspace when the method
finishes execution.

> fprintf('%d\n',obj.value());
> end
>
> How do I make it so that the function doubler can permanently change an
> object of exclass' value property?

Either return the modified object from the doubler method or (IF it makes
sense for your object as a whole to have handle semantics) make your object
a handle object.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brfylq3.html

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Eric He on
> Either return the modified object from the doubler method or (IF it makes
> sense for your object as a whole to have handle semantics) make your object
> a handle object.

Thank you for your response, but I still am unsure of how to continue. I don't believe that I should make the object a handle object, but I am unsure of how to return the modified object from the doubler method. I tried changing doubler to:

function obj=doubler(obj)

obj.value=obj.value*2;
fprintf('%d\n',obj.value());
end

But this didn't help.

Thanks again,

Eric
From: Steven Lord on

"Eric He" <ericjhe(a)hotmail.com> wrote in message
news:hu8ssa$5k1$1(a)fred.mathworks.com...
>> Either return the modified object from the doubler method or (IF it makes
>> sense for your object as a whole to have handle semantics) make your
>> object a handle object.
>
> Thank you for your response, but I still am unsure of how to continue. I
> don't believe that I should make the object a handle object, but I am
> unsure of how to return the modified object from the doubler method. I
> tried changing doubler to:
>
> function obj=doubler(obj)
>
> obj.value=obj.value*2;
> fprintf('%d\n',obj.value());
> end
>
> But this didn't help.

Modifying the method signature is half of what you need to do. You also
need to _call_ the method with an output argument.

x = doubler(x);

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Eric He on
Thanks! This worked great!

Eric