MATLAB Answers

using a nested for loop to edit a structure

1 view (last 30 days)
Elissa Moller
Elissa Moller on 29 May 2020
Answered: Arthur Goldsipe on 1 Jun 2020
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)
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)
Stephen on 30 May 2020
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:

Sign in to comment.

Answers (1)

Arthur Goldsipe
Arthur Goldsipe on 1 Jun 2020
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.

Community Treasure Hunt

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

Start Hunting!