Unrecoginized Fieldname although it should be correctly defined
조회 수: 38 (최근 30일)
이전 댓글 표시
Hi,
Im trying to create a for loop to shorten my code, but i am running into some issues. My code is as followed:
% Define the joint names
jointNames = {'Knee', 'Ankle', 'Hip', 'Shoulder', 'Elbow'};
% Define the movement directions
directions = {'maxX', 'minX', 'maxY', 'minY', 'maxZ', 'minZ'};
% Initialize a structure to hold the mean angles
meanAngles = struct();
% Loop through each joint and direction
for j = 1:length(jointNames)
jointName = jointNames{j};
for d = 1:length(directions)
direction = directions{d};
% Construct the field name for the data
fieldName = ['RangeAnglesLeft.' direction '.' jointName];
% Calculate the mean angle for each condition (Nwalk and Wwalk)
meanAngleN = mean(cell2mat(Nwalk.(fieldName)));
meanAngleW = mean(cell2mat(Wwalk.(fieldName)));
% Store the mean angles in the structure
fieldMaxName = ['Max' jointName(1:end-1) direction];
fieldMinName = ['Min' jointName(1:end-1) direction];
meanAngles.(fieldMaxName).(['N' fieldMinName]) = meanAngleN;
meanAngles.(fieldMinName).(['W' fieldMaxName]) = meanAngleW;
end
end
Somehow there is an error with the fieldname. The error states: Unrecognized field name "RangeAnglesLeft.maxX.Knee".
Nwalk and Wwalk are a large dataset containing RangeAnglesLeft. This variable is a struct which can retrieve data by being called out as followed: Nwalk.RangeAnglesLeft.maxX.Knee. Somehow in the code above, it tells me that RangeAnglesLeft.maxX.Knee is not recognized. Can anyone help me with this problem?
댓글 수: 1
Stephen23
2023년 8월 14일
"This variable is a struct which can retrieve data by being called out as followed: Nwalk.RangeAnglesLeft.maxX.Knee"
No, that is exactly the problem: what you have are nested structures, not a structure. Any structure that happens to be stored inside another structure is still its own structure, which requires its own field references.
Once you understand that, then this mistake is easy to avoid.
답변 (2개)
Florian Bidaud
2023년 8월 14일
편집: Florian Bidaud
2023년 8월 14일
You can't use dots in your fieldname. The dot creates a new field in the structure, but it can't be done directly in the name of the field with a char array or a string.
Instead of
fieldName = ['RangeAnglesLeft.' direction '.' jointName];
% Calculate the mean angle for each condition (Nwalk and Wwalk)
meanAngleN = mean(cell2mat(Nwalk.(fieldName)));
You should do something like:
meanAngleN = mean((Nwalk.RangeAnglesLeft.(direction).(jointName));
댓글 수: 0
Steven Lord
2023년 8월 14일
As others have said, you cannot index into a multi-level struct array using the .() notation like that. For this you'd probably want to use getfield.
A = struct('x', struct('b', 2, 'c', 3), 'y', 42)
fieldsToRetrieve = {'x', 'c'};
three = getfield(A, fieldsToRetrieve{:}) % Equivalent of A.x.c
check = A.x.c
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!