using a nested for loop to edit a structure
3 次查看(过去 30 天)
显示 更早的评论
I am trying to edit the tempFactor field in a PDBStruct. I want to use row 1 of matrix4 to search the AtomSerNo field and when they are equal replace the corresponding tempFactor with row 2 of matrix4. This is the code I tried
for i3=1:length(matrix3)
for i4=1:length(matrix4)
if PDBStruct.Model.Atom.AtomSerNo(i3)==matrix4(1,i4)
PDBStruct.Model.Atom.tempFactor=matrix4(2,i4);
end
end
end
This is the error I got "Expected one output from a curly brace or dot indexing expression, but there were 30471 results"
I think I just dont understand how to properly search within the structure. This format worked great when I was editing a matrix rather than a structure earlier in my code (example below)
for i1=1:size(matrix1(:,1))
for i2=1:size(matrix2(:,1))
if matrix1(i1,1)==matrix2(i2,1)
matrix1(i1,3)=matrix2(i2,2);
end
end
end
2 个评论
Stephen23
2020-5-30
As an alternative to nested loops you could use both outputs of ismember:
for which you will also need to concatenate those values into vectors:
回答(1 个)
Arthur Goldsipe
2020-6-1
As mentioned in the comments, if you want to compare one atom at a time, the proper way to index is Atom(i3).AtomSerNo. But if you want to do the comparison more efficiently, you can "vectorize" the operation to work across all atoms at once using ismember. The way that I would assign values from matrix4 at once requires me to put them all in a temproary cell array. I would write that code something like this:
[serNoIsInMat4, locationInMat4] = ismember([PDBStructmodel.Model.Atom.AtomSerNo], matrix4(1,:));
tempFactorCell = num2cell(matrix4(2,locationInMat4(serNoIsInMat4)));
[PDBStructmodel.Model.Atom(serNoIsInMat4).tempFactor] = tempFactorCell{:};
There might be some mistakes in my code. Since I don't have access to your data, it's hard for me to confirm I wrote things properly. But hopefully this is enough to help you understand how to proceed.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!