variable.(key) what is?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

variable.(key) what is?

Octave - General mailing list
hi, i see in example code the .()  after variable

Example:

function v = mod09_05_state(key, val)
  persistent state;
  if (nargin == 0)  
    % Return the entire struct
     printf("0 \n" );
    v = state;
  elseif (nargin == 1)
    % Return a particular field from the struct
    printf("1 \n" );
    v = state.(key);
  elseif (nargin == 2)
      printf("2 \n" );
    % Set a key value (key must be a string)
    state.(key) = val;
    v = val;
  end
end

what is  .(key)? i dont find in help octave variable



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: variable.(key) what is?

apjanke-floss


On 12/19/19 2:21 PM, shivax via Help list for GNU Octave wrote:

> hi, i see in example code the .()  after variable
>
> Example:
>
> function v = mod09_05_state(key, val)
>   persistent state;
>   if (nargin == 0)  
>     % Return the entire struct
>      printf("0 \n" );
>     v = state;
>   elseif (nargin == 1)
>     % Return a particular field from the struct
>     printf("1 \n" );
>     v = state.(key);
>   elseif (nargin == 2)
>       printf("2 \n" );
>     % Set a key value (key must be a string)
>     state.(key) = val;
>     v = val;
>   end
> end
>
> what is  .(key)? i dont find in help octave variable
>

It's dynamic field/property access, used on structs and objects. `name =
"foo"; s.(name)` is the same as `s.foo`. Example:

>> s.foo = 42
s =
  scalar structure containing the fields:
    foo =  42

>> fieldname = "foo";
>> s.(fieldname)
ans =  42
>> s.foo
ans =  42
>>

You can also use it to make structs with weird field names (but you
probably shouldn't):

>> s.("foo bar") = 12345
s =
  scalar structure containing the fields:
    foo =  42
    foo bar =  12345

Cheers,
Andrew


Reply | Threaded
Open this post in threaded view
|

Re: variable.(key) what is?

Octave - General mailing list
Reply | Threaded
Open this post in threaded view
|

Re: variable.(key) what is?

brillmindz
In reply to this post by apjanke-floss
Within a data set, there may be one or more variables that can uniquely
identify an observation. These variables act as keys into the variable
collection and are therefore called key variables.



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html