help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to modify structure fields inside functions


From: c.
Subject: Re: How to modify structure fields inside functions
Date: Sun, 23 Sep 2012 21:13:22 -0000


On 6 Apr 2012, at 11:23, Bruno Picasso wrote:

Hi everybody,

I need your help to understand how to modify a structure field inside a function. For example:

a.b=3
a =
{
 b =  3
}

function modify (s)
setfield(s,'b',5)
endfunction

modify(a)
ans =
{
 b =  5
}

a.b
ans =  3


Thanks for your kind help

Bruno

that cannot be done,
all functions in the octave/matlab 
language have pass-by-value semantics, 
so you have to do it like this:

a.b = 3;

function res = modify (s)
setfield (s,'b',5); %% or, equivalently, s.b = 5;
endfunction

a = modify (a);
a.b
ans =  5

c.


P.S. an alternative would be to use global variables



reply via email to

[Prev in Thread] Current Thread [Next in Thread]