[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to modify structure fields inside functions
From: |
PhilipNienhuis |
Subject: |
Re: How to modify structure fields inside functions |
Date: |
Fri, 6 Apr 2012 03:36:56 -0700 (PDT) |
ponipei 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
>
>
You trapped into forgetting the assignment. If you don't assign a function
result to a variable, Octave assigns it to dummy variable "ans".
You should do:
a = modify (a)
IOW, Octave's language doesn't have "call by reference".
Philip
--
View this message in context:
http://octave.1599824.n4.nabble.com/How-to-modify-structure-fields-inside-functions-tp4537009p4537107.html
Sent from the Octave - General mailing list archive at Nabble.com.