using a nested for loop to edit a structure
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Stephen23
il 30 Mag 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:
Risposte (1)
Arthur Goldsipe
il 1 Giu 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!