Hopefully my problems stem from a lack of understanding of Matlab class
syntax rather than a limitation of Matlab classes.
The following class 'testclass' has constructor which sets a single
data member 'val':
* function obj = testclass( arg )
* if nargin == 0
* obj.val = 0;
* obj = class( obj, 'testclass' );
* elseif isa( arg, 'amer' )
* obj = arg;
* else
* obj.val = arg;
* obj = class( obj, 'testclass' );
* end
I created get_val and set_val methods to retrieve and set the 'val'
data member respectively as follows:
* function ret = get_val( this )
* ret = this.val;
* function set_val( this, arg )
* this.val = arg;
get_val correctly returns the data members that were set in a call to
the constructor. For example:
* » x = testclass( 100 );
* » get_val( x )
*
* ans =
*
* 100
However, a call to set_val() does not seem to change the data member.
For example:
* » x = testclass(100);
* » set_val( x, 50 );
* » get_val( x )
*
* ans =
*
* 100
Can anyone explain whether I am being a pinhead, or if there is in fact
a limitation in Matlab class usage that I am encountering.
Thanks much!
Brett
Sent via Deja.com http://www.deja.com/
Before you buy.
The problem is that the assignment in the set_val function only affects the
variable in set_val's workspace. You need to use the assignin statement to
make sure the set_val function affects the variable in the workspace from
which the function was called.
See http://www.mathworks.com/support/solutions/v5/22951.shtml for details
and some sample code. I actually ran into the answer for that problem
yesterday while poking around for something else in Mathwork's support area.
Timing is everything. :)
- Jordan
<bre...@planetkc.com> wrote in message news:7uhq1d$8da$1...@nnrp1.deja.com...
when you call set_val(x,10) MATLAB passes a copy of x to the function. You
do have access to the attibutes, but when you leave the function the object
is destroyed and your changes are lost.
function x_out = set_val(x,val)
x_out=x;
x_out.val=val;
and the call
x=set_val(x,10)
will do the trick
Sascha
* function set_val( this, arg )
* this.val = arg;
* assignin(´caller´,inputname(1),this)
look at the help for more
Veit
bre...@planetkc.com wrote:
> Once data members (struct values) are set in a Matlab class
> constructor, can they be changed in class methods?
>
> ...
--
************************************************************
Dipl.-Ing. Veit-Stefan Kafka UniBw-Muenchen ET 1
mailto:Veit....@UniBw-Muenchen.de
Fon: +49 (0) 89 / 60 04 41 54
Fax: +49 (0) 89 / 60 04 36 13
************************************************************
> * function ret = get_val( this )
> * ret = this.val;
>
> * function set_val( this, arg )
> * this.val = arg;
As others already indicated, your results stem from the fact that the
set_val function only changes the local copy of "this". Two solutions
were suggested
1) use evalin in combination with inputname. Note however that this
does not work when the first argument is not a single variable but an
element of an array, such as set_val(obj(3),50).
2) use newthis = set_val (this, arg). Instead of this solution I would
suggest to overload the subsasgn and subsref commands, like this
function newthis = subsasgn(this,ref,value)
newthis = this
switch ref(1).type,
case '.',
switch ref(1).subs
case 'val',
newthis.val=value;
otherwise,
error('Invalid object property');
end;
otherwise, % '()' '{}'
error('Unexpected assignment.');
end;
function value = subsref(this,ref)
switch ref(1).type,
case '.',
switch ref(1).subs
case 'val',
value=this.val;
otherwise,
error('Invalid object property');
end;
otherwise, % '()' '{}'
error('Unexpected assignment.');
end;
ans put these functions in your @testclass directory.
Now, you should be able to get the following result:
» x = testclass( 100 );
» x.val
ans =
100
» x.val=50;
» x.val
ans =
100
Hope this helps,
Best regards,
Bert Jagers
for example, if you use a try/catch block:
try
cool_object = set_val(cool_object,val)
catch
disp('value out of range');
end;
you dont have to "unwind" anything since cool_object is only
assigned if set_val(...) is successful.
most likely, this is as fast, or a bit faster than using
assignin(..); most likely, it wont clutter your code,
and the "pass by value" semantics of MATLAB are left alone.
this means that all the code works the same way.
playing with subsref and subasgn IS MESSY, and if the
indexing expression is anything more complex than:
object.a = 3;
then life gets complicated. (example: object.a(1).name = 'ted')
the MATLAB enviroment is much better at navigating syntax
than a "homebrewed" one.
i think that using :
data = load('mynumbers.mat')
is also much, much, better from a code maintainece standpoint
since it is "black and white" what happenning;