How to rectify "arrays have incompatible sizes for this operation error" in this code: (Bioinformatics-related)

1 回表示 (過去 30 日間)
ubq=getpdb('1UBQ')
ubq = struct with fields:
Header: [1×1 struct] Title: 'STRUCTURE OF UBIQUITIN REFINED AT 1.8 ANGSTROMS RESOLUTION' Compound: [4×20 char] Source: [4×34 char] Keywords: 'CHROMOSOMAL PROTEIN' ExperimentData: 'X-RAY DIFFRACTION' Authors: 'S.VIJAY-KUMAR,C.E.BUGG,W.J.COOK' RevisionDate: [1×5 struct] Journal: [1×1 struct] Remark1: [1×1 struct] Remark2: [1×1 struct] Remark3: [1×1 struct] Remark4: [2×59 char] Remark100: [2×59 char] Remark200: [49×59 char] Remark280: [6×59 char] Remark290: [32×59 char] Remark300: [6×59 char] Remark350: [13×59 char] Remark500: [41×59 char] Remark525: [15×59 char] DBReferences: [1×1 struct] Sequence: [1×1 struct] Formula: [1×1 struct] Helix: [1×2 struct] Sheet: [1×5 struct] Cryst1: [1×1 struct] OriginX: [1×3 struct] Scale: [1×3 struct] Model: [1×1 struct] Master: [1×1 struct] SearchURL: 'https://files.rcsb.org/download/1UBQ.pdb'
for i=1:1000
if ubq.Model.Atom(i).AtomName == 'CA'
b=[ubq.Model.Atom(i).X,ubq.Model.Atom(i).Y,ubq.Model.Atom(i).Z]
end
end
b = 1×3
26.2660 25.4130 2.8420
b = 1×3
26.8500 29.0210 3.8980
Arrays have incompatible sizes for this operation.

採用された回答

Arthur Goldsipe
Arthur Goldsipe 2023 年 1 月 27 日
Let me just start by sharing the fixed code. (I also fixed a bug in the first for loop.)
ubq=getpdb('1UBQ');
for i=1:numel(ubq.Model.Atom)
if ubq.Model.Atom(i).AtomName == "CA"
b=[ubq.Model.Atom(i).X,ubq.Model.Atom(i).Y,ubq.Model.Atom(i).Z];
end
end
b
b = 1×3
40.3730 39.8130 33.9440
Text in single quotes is a character vector. In this case, == will compare the two characters in CA one by one and error as you see if AtomName does not have exactly two characters. Also note that the result of this comparison will be a two-element logical vector with the result for each character. For example:
'Ca' == 'CA'
ans = 1×2 logical array
1 0
This behavior was introduced in the very first version of MATLAB. Character arrays are treated much like numeric arrays. And that was consistent with the focus of MATLAB on arrays in the early days. I should also mention another option here. You could instead compare two character vectors using the strcmp function:
strcmp('Ca','CA')
ans = logical
0
Now, this confused a lot of people. And wasn't very convenient for the kinds of text processing we do today. So a few years ago, MATLAB introduce a new way to store text: strings. That's what "CA" is. And strings behave more like what you would expect when processing text, including == meaning that you compare the entire contents of the string and return a single true or false indicating whether the text matches. You can find more information in MATLAB's documentation here.
Lastly, let me show you how I'd write this to avoid the for loop entirely and present all the results in a single matrix
isCA = ({ubq.Model.Atom.AtomName} == "CA");
caAtoms = ubq.Model.Atom(isCA);
coordinates = [caAtoms.X; caAtoms.Y; caAtoms.Z]'
coordinates = 76×3
26.2660 25.4130 2.8420 26.8500 29.0210 3.8980 26.2350 30.0580 7.4970 26.7720 33.4360 9.1970 28.6050 33.9650 12.5030 27.6910 37.3150 14.1430 30.2250 38.6430 16.6620 29.6070 41.1800 19.4670 31.4220 43.9400 17.5530 28.9780 43.9600 14.6780

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by