Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Object-Oriented programming question

44 views
Skip to first unread message

Matthew Halfant

unread,
Dec 11, 2003, 9:17:07 PM12/11/03
to
I built a complex "object" using tlist, but was surprised to discover that I
couldn't modify the object's fields (members) in a function. For example,

-->x = tlist(['foo', 'A'], rand(2,2));

In truth I have a non-trivial object (an LCD "panel") whose fields are
initialized by reading matrices from files, but the above example will serve
for illustration.

I'd like to write a function that accepts x as an argument and modifies its
data, e.g.

function y = change_foo(x); x.A = rand(3,3); y = x; endfunction

If I make the call

-->y = change_foo(x);

the x will not be changed, but y will be what I wanted x to be. I can, of
course, change x at the top level:

-->x.A = rand(3,3);

but not within a function -- the function only gets a "copy" to work on.

Is there a way to get Scilab to do what I want?

Thanks,
Matthew


Bruno Pincon

unread,
Dec 12, 2003, 1:34:42 AM12/12/03
to

The only way I see (staying in pure scilab language) is by :

-->x = change_foo(x);

This is because a function is not allowed to modify
input arguments (but this would be possible with a C interfaced
function). Also I would say that reading a tlist-member is
efficient but modifying one field is not (at least if the
new "value" have not the same "length" as the old one).
Despite this, tlist are nevertheless practical.

hth
Bruno

Alexander Vigodner

unread,
Dec 12, 2003, 8:45:35 AM12/12/03
to
"Matthew Halfant" <hal...@aol.com> wrote in message news:<Dy9Cb.10675$rP6....@newsread2.news.pas.earthlink.net>...

The way I know is tricky, since scilab does not know about name of
argument, it is working with the copy. If you pass the name of input
variable too (as string)then you you can do the following
function []=change_foo(x,name)
<All your operations with x>
//at the dend:
exestr(name+'=resume(x)')
endfunction

0 new messages