- m@cortex
- Tutorial
8. working with @key objects
Let's first create a single-key default object:
>> k = key;
Interaction with a @key object is done with the same object-dot-method or object-dot-property syntax used with the @cortex class. For example, the structure of k
can be displayed in the command window in the following way:
>> k.print;
Content of the @key object:
=======================================
sta fin cue con blo res tri typ exp rep
Key #1: noname
-50 250 [ 23] [ -1] [ -1] [ 0] [ -1] [ -1] [ -1] [ -1]
You can interactively access and change any of the keys' properties, and it is possible to add new keys to an existing @key object. For example:
>> k.cue % returns the key's cue event(s)
ans =
23
>> k.conds = 10:12; % changes the key's condition range
>> k.conds
ans =
10 11 12
>> k2 = key; % create and modify another @key object
>> k2.label = 'anotherkey';
>> k2.con = 20;
>> k = k + k2; % concatenate k and k2
Things are only slightly more complicated when the @key object contains 2 or more keys (and possibly subkeys). The following examples are based on a sample keyfile. First, create the @key object and assign it to the variable k
:
>> k = key ('samplekeyfile.key');
>> k.pri
Content of the @key object:
=======================================
sta fin cue con blo res tri typ exp rep
Key #1: uno
-125 675 [ 25] [ 25 26] [ -1] [ 0] [ -1] [ -1] [ -1] [ -1]
Key #2: due
-125 675 [ 27] [ 27 28] [ -1] [ 0] [ -1] [ -1] [ -1] [ -1]
-125 675 [ 25] [ 29 30] [ -1] [ 0] [ -1] [ -1] [ -1] [ -1]
Key #3: tre
-125 675 [ 27] [ 31 32] [ -1] [ 0] [ -1] [ -1] [ -1] [ -1] ;
This object contains 3 keys, and the 2nd key is complex, because it is made of 2 subkeys. This is also shown by the following two properties:
>>k.length % returns the number of keys in the object
ans =
3
>>k.size % returns the number of subkeys in each key
ans =
1 2 1
In order to retrieve (or set) attributes, you must specify (in parens) the key number:
>> k(3).conds % returns the condition range of the 3rd key
ans =
31 32
However, when a key is complex, then:
>> k(2).conds
Warning: this is a complex key; returning value for 1st subkey only
> In c:\users\giuseppe\docs\matlab\giucode\@key\subsref.m at line 139
ans =
27 28
The proper way to access properties of complex keys is to use object(key, subkey)
; for example:
>> k(2,2).cond % returns the condition range of subkey #2 of key #2
ans =
29 30
Use the same syntax to change key/subkey values.
Note that if you concatenate two @key objects (k3 = k1 + k2)
and the label of the last key in k1
is identical to that of the first key in k2
, then these 2 keys are merged together into a complex one.