[S.Area] not supported for code generation

hey guides i use this function on the simulink but show the error:Referencing a component from array of non-scalar values is not supported for code generation.
Function 'MATLAB Function' (#35.204.212), line 8, column 25:
"[S.Area]"
Launch diagnostic report.
what can replace this to success simulation on simulink.
function y = areaimg(u)
bw2 = zeros(240,320);
ss = 20;
B = 6000;
img_bw2 = bwareaopen(u,ss,8);
[L2,num2]=bwlabel(img_bw2,8);
S = regionprops(L2,'Area');
bw2 = ismember(L2, find([S.Area] < B));
y = bw2;
end

 採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 26 日
編集済み: Walter Roberson 2017 年 10 月 26 日

0 投票

Just loop. You are generating code anyhow, so the loop should be efficient.
Have you considered using bwareafilt() instead ?

6 件のコメント

tsai kai shung
tsai kai shung 2017 年 10 月 26 日
but The function 'bwareafilt' is not supported for standalone code generation.
Walter Roberson
Walter Roberson 2017 年 10 月 26 日
Ah. Well, loop then.
tsai kai shung
tsai kai shung 2017 年 10 月 27 日
how to loop then?
Walter Roberson
Walter Roberson 2017 年 10 月 27 日
編集済み: Walter Roberson 2017 年 10 月 27 日
bw2 = false(size(L2));
for K = 1 : num2
if S(K).Area >= B
bw2( L2 == K) = true;
end
end
tsai kai shung
tsai kai shung 2017 年 10 月 27 日
thanks for your help. by the way the false(size(L2))=zeros(size(L2))?
Walter Roberson
Walter Roberson 2017 年 10 月 27 日
false() and zeros() are not the same: false() creates an array of type logical and zeros() creates an array of type double.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2017 年 10 月 27 日

0 投票

What is num2? Is it 0, and therefore S is empty? If S is empty, then trying to access S.Area would throw an error.
Image Analyst
Image Analyst 2017 年 10 月 27 日

0 投票

The whole function can be done with this single line of code
y = bwareafilt(u, [1, 6000]);

2 件のコメント

tsai kai shung
tsai kai shung 2017 年 10 月 27 日
but this function not use on the simulink
tsai kai shung
tsai kai shung 2017 年 10 月 27 日
what function can replace?

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

Community Treasure Hunt

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

Start Hunting!