Solo: Subscript syntax

From Bcontrol
Revision as of 00:34, 18 March 2009 by CarlosBrody (talk | contribs) (Matrix subscripts)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Cell subscripts[edit]

As mentioned above, SoloParamHandles can have a value that is any of the types that Matlab handles. This includes matrices and cells. For example, we could say:

  >> SoloParamHandle(‘base’, ‘myvar’, ‘value’, cell(3,1));
  >> value(myvar)
  ans = 
     []
     []
     []


That is, we’ve created a SoloParamHandle whose value is a 3-by-1 cell, the elements of which are all empty. Suppose we wanted the first element to be the string ‘foo’ and the second element to be the number 7, while leaving the third element unchanged. We could do this:

  >> v = value(myvar); v{1} = ‘foo’; v{2} = 7; myvar.value = v;
  >> value(myvar)
  ans = 
     ‘foo’
     [  7]
     []

However, once again Matlab syntax allows making things a little more readable. When a curly brace is right next to a SoloParamHandle, Matlab knows that this means “oh, the value of the SoloParamHandle must be a cell, and the user is trying to access the contents of the cell.” So we can actually write the much more direct lines:

  >> myvar{1} = ‘foo’; myvar{2} = 7;
  >> value(myvar)
  ans = 
     ‘foo’
     [  7]
     []

Similarly, if you just ask for myvar{1}, Matlab knows how to interpret this—you’re trying to access the contents of a SoloParamHandle whose value is a cell:

  >> myvar{1}
  ans = 
  foo


You are not limited to just cells that are vectors (i.e., have one subscript). They can have as many as Matlab allows. Thus, for example, “myvar{2,3}” is perfectly valid syntax.

If you use the curly bracket notation when the value of a SoloParamHandle is not a cell, you will, of course, get an error.

Matrix subscripts[edit]

Exactly the same process applies to matrix subscripts with parentheses instead of curly brackets. For example, you can do this:

  >> myvar.value = zeros(2,2);
  >> myvar(1,2) = 3;
  >> myvar(2,1) = 1;
  >> value(myvar)
  ans =
  0	3
  1	0


For both cells and matrices, you can also use the "end" form, as in

  >> myvar.value = [1 2 3];
  >> myvar(end)
  ans = 
     3