12

I have a predefined array of 20 position which correspond to 20 joint out of my body. The joints are marked with string values (e.g. 'left_knee', 'head', 'left_eye', etc.).

I want to refer to a certain value within the array by using the attached string value. For example I want to store the position of the observed joints. Initially all the position within the array are (-1 , -1) and then if I spot a certain joint I want to do something like Joints('left_knee') = [100 200].

How can I do this in Matlab?

3 Answers 3

13

How about structs?

Joints.left_knee = [100 200];
Joints.head      = [-1 -100];

Get all fields with fieldnames, refer to individual entries dynamically like so:

someVar = 'left_eye';
Joints.(someVar) = [200 250];

etc. If you happen to have multiple joints, all needing the same sort of data but they all belong to the same system, you can make multi-D structs, too:

Joints(1).left_knee = [100 200];
Joints(1).head      = [-1 -100];

Joints(2).left_knee = [200 450];
Joints(2).head      = [-10 -189];

Joints(3).left_knee = [-118 264];
Joints(3).head      = [+33  78];

Just to show you some techniques useful in the context of multi-D structs:

>> someVar = 'head';
>> Joints.(someVar)     % will be expanded cell-array, 1 entry for each dimension
ans =
    -1  -100
ans =
   -10  -189
ans =
    33    78
>> [Joints.(someVar)]   % will collect those data in regular array
ans =
    -1  -100   -10  -189    33    78
>> {Joints.(someVar)}   % will collect those data in cell array
ans = 
    [1x2 double]    [1x2 double]    [1x2 double]
>> [A,B,C] = deal(Joints.(someVar));  % will assign data to 3 separate vars
A =
    -1  -100
B =
   -10  -189
C =
    33    78

Type help struct for more info and learn about relevant functions.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Nice, I didn't know you could refer to struct fields with a string variable.
9

You can use the built-in map containers to map values to unique keys. Here's a quick example for you:

valueSet = {[-1, 1], [2, 3], [1,5], [1,8]};
keySet = {'left_knee', 'right_knee', 'left_eye', 'right_eye'};
Joints = containers.Map(keySet,valueSet);
Joints('left_knee')

ans =
    -1     1

You can get the values/keys of the map object as follows:

values(Joints)

ans = 
    [1x2 double]    [1x2 double]    [1x2 double]    [1x2 double]

keys(Joints)

ans = 
    'left_eye'    'left_knee'    'right_eye'    'right_knee'

See the documentation for more 'examples and how to'.

Comments

2

To expand @RodyOldenhuis solution, you can also store your indexes in a struct array and use a plain matrix for the data. This way you can still apply normal matrix operation to all the data vectorwise:

idx.left_knee  = 1;
idx.right_knee = idx.left_knee + 1;
idx.left_elbow = idx.right_knee+ 1;
idx.right_elbow= idx.left_elbow+ 1;

joint_pos = rand(4,8);
t = linspace(0,1,8);
joint_vel = diff(joint_pos,2)./diff(t);

joint_vel(idx.right_knee,:)

In my example I set up a data structure for position and velocity of 4 joints at 8 points in time to illustrate my technique.

I find this easier to work with when using multi-dimensional datasets. Of course it depends on your application, if you for example just want to store constants, @RodyOldenhuis' solution obviously is much simpler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.