How to brows variable names of a structure and select the closest to a given string?

6 views (last 30 days)
Hi there,
I imported motion capture data to Matlab in the form of a structure.
e.g. Experiment1 file:
data.head_12.time; data.head_12.data
data.arm_4.time; data.arm_4.data
...
Sometimes the index allocated to the marker sets changes between experiments, which results in a change of the variable name in the structure:
e.g. Experiment2 file:
data.head_16.time; data.head_16.data
data.arm_11.time; data.arm_11.data
...
How can I select the closest variable name without knowing the index?
myvariable = find(data.filednames, 'head_xx').data;
  3 Comments
M.B
M.B on 17 Oct 2024
I understand your point. Unfortunately, I have no control over the naming of variables, which is done in the MOCAP software.

Sign in to comment.

Accepted Answer

Sameer
Sameer on 17 Oct 2024
Edited: Sameer on 17 Oct 2024
Hi
To select the closest variable name without knowing the exact index, you can use "dynamic field names" and "regular expressions" to match the desired pattern.
Here's how you can do this:
1. Use "fieldnames()" to get all field names from the structure.
2. Use "regexp()" to find the field name that matches your pattern.
3. Access the data using dynamic field names.
Sample Code :
data.head_12.time = 0:0.1:10; % Sample time data
data.head_12.data = sin(data.head_12.time); % Sample data
data.arm_4.time = 0:0.1:10; % Sample time data
data.arm_4.data = cos(data.arm_4.time); % Sample data
data.leg_7.time = 0:0.1:10; % Sample time data
data.leg_7.data = tan(data.leg_7.time); % Sample data
% Now, let's find the field that matches 'head_xx'
fields = fieldnames(data);
pattern = '^head_\d+$'; % Pattern to match 'head_' followed by digits
matches = regexp(fields, pattern, 'match');
matchedField = fields(~cellfun('isempty', matches));
if ~isempty(matchedField)
selectedField = matchedField{1};
myTime = data.(selectedField).time;
myData = data.(selectedField).data;
disp(['Selected field: ', selectedField]);
disp('Time data:');
disp(myTime);
else
error('No matching field found for pattern: %s', pattern);
end
Selected field: head_12
Time data:
Columns 1 through 18 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 Columns 19 through 36 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 Columns 37 through 54 3.6000 3.7000 3.8000 3.9000 4.0000 4.1000 4.2000 4.3000 4.4000 4.5000 4.6000 4.7000 4.8000 4.9000 5.0000 5.1000 5.2000 5.3000 Columns 55 through 72 5.4000 5.5000 5.6000 5.7000 5.8000 5.9000 6.0000 6.1000 6.2000 6.3000 6.4000 6.5000 6.6000 6.7000 6.8000 6.9000 7.0000 7.1000 Columns 73 through 90 7.2000 7.3000 7.4000 7.5000 7.6000 7.7000 7.8000 7.9000 8.0000 8.1000 8.2000 8.3000 8.4000 8.5000 8.6000 8.7000 8.8000 8.9000 Columns 91 through 101 9.0000 9.1000 9.2000 9.3000 9.4000 9.5000 9.6000 9.7000 9.8000 9.9000 10.0000
Please refer to the below MathWorks documentation links:
Hope this helps!
  2 Comments
M.B
M.B on 18 Oct 2024
It did work with a small tweek.
The variable names were a bit more complex than the simple example I provided.
E.g.
data.head_12_x.time
data.head_12_y.time ...
I searched for the follwoing match "head_" because the I only have one head and the sufix after the index is always in this order: x y z r p w. So I know that the first match is X etc.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!