using a nested for loop to edit a structure

9 ビュー (過去 30 日間)
Elissa Moller
Elissa Moller 2020 年 5 月 29 日
回答済み: Arthur Goldsipe 2020 年 6 月 1 日
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 件のコメント
Elissa Moller
Elissa Moller 2020 年 5 月 30 日
this works but it runs for a really long time
for i3=1:length(matrix3)
for i4=1:length(matrix4)
if PDBStructmodel.Model.Atom(i3).AtomSerNo==matrix4(1,i4)
PDBStructmodel.Model.Atom(i3).tempFactor=matrix4(2,i4);
end
end
end
Any suggestions?
Stephen23
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
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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by