Hey everyone,
I've been trying to improve my coding lately with for loops, but I'm sturggling to figure this one out.
Here's the original code. BW2_BW refers to an image and the row's all refer to image coordinates.
R1 = imcrop(BW2_BW, [row_1]);
R2 = imcrop(BW2_BW, [row_2]);
R3 = imcrop(BW2_BW, [row_3]);
R4 = imcrop(BW2_BW, [row_4]);
R5 = imcrop(BW2_BW, [row_5]);
stats_BW_R1 = regionprops(R1,'Centroid');
stats_BW_R2 = regionprops(R2,'Centroid');
stats_BW_R3 = regionprops(R3,'Centroid');
stats_BW_R4 = regionprops(R4,'Centroid');
stats_BW_R5 = regionprops(R5,'Centroid');
This is what I'm trying to do:
for k = 1:5
stats_BW_R(k) = regionprops(R(k),'Centroid');
end
I basically want k to replicate values 1:5 to replace what my original code did. What am I doing wrong?
Thanks!

1 件のコメント

Stephen23
Stephen23 2020 年 6 月 4 日
"What am I doing wrong?"
Numbering some variables and then trying to access the variable names dynamically.

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2020 年 6 月 4 日

1 投票

stats_BW_R = cell(1,5);
for k = 1:5
R = imcrop(BW2_BW, rows{k}); % rows = [row_1,...row_5]
stats_BW_R{k} = regionprops(R,'Centroid');
end

4 件のコメント

DP
DP 2020 年 6 月 4 日
Okay, for the rows function, do you create a cell or a double? I did as you commented and I get the error Brace indexing is not supported for variables of this type.
madhan ravi
madhan ravi 2020 年 6 月 4 日
編集済み: madhan ravi 2020 年 6 月 4 日
rows = {row_1,...row_5}
You need to learn the basics of MATLAB.
DP
DP 2020 年 6 月 4 日
I agree
madhan ravi
madhan ravi 2020 年 6 月 4 日
And make sure to read the comment of Fangjun and Stephens.

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2020 年 6 月 4 日

1 投票

Don't create variables R1, R2, ... R5. Instead, create array R, use R(1), R(2), ... R(5). Then you can run your for-loop.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

DP
2020 年 6 月 4 日

コメント済み:

2020 年 6 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by