Problem with 'Logical indexing requires support of variable-sized arrays, which is currently disabled'

Hello Dears,
I have written the below code in .m file and it was OK. But for one part of my work, I should put it in Embedded function in SIMULINK, and as i run it this error appears. I would be appreciated if anybody can help me.
for j=1:5
if j==1 && pmuA9 >=0 && pmuA8 >=0 && pmuA7 >=0 && pmuA6 >=0 && pmuA5 >=0 && pmuA4 >=0 && pmuA3 >=0 && pmuA2 >=0 && pmuA1 >=0
summuA_Q1=muA_9(j)+muA_8(j)+muA_7(j)+muA_6(j)+muA_5(j)+muA_4(j)+muA_3(j)+muA_2(j)+muA_1(j);
sumEyimuA_Q1=EyimuA_9(j)+EyimuA_8(j)+EyimuA_7(j)+EyimuA_6(j)+EyimuA_5(j)+EyimuA_4(j)+EyimuA_3(j)+EyimuA_2(j)+EyimuA_1(j);
yPFLS_Q1=(sumEyimuA_Q1)/(summuA_Q1);
yPFLS_Q1(isnan(yPFLS_Q1))=0;
PyQ1=[PmuA_9(j) PmuA_8(j) PmuA_7(j) PmuA_6(j) PmuA_5(j) PmuA_4(j) PmuA_3(j) PmuA_2(j) PmuA_1(j)];
PyQ1nonZERO=nonzeros(PyQ1);
PyQ1nonZERO(isempty(PyQ1nonZERO))=0;
PyPFLS_Q1=max(PyQ1nonZERO);
end
end
The error is:
Logical indexing requires support of variable-sized arrays, which is currently disabled.
Function 'Embedded MATLAB Function' (#22.38943.38960), line 910, column 23:
"isnan(yPFLS_Q1)"

 채택된 답변

PyQ1nonZERO = nonzeros(PyQ1);
if isempty(PyQ1nonZERO)
PyPFLS_Q1 = 0;
else
PyPFLS_Q1 = max(PyQ1nonZERO);
end

댓글 수: 4

Thanks for your response. But the error is on 'yPFLS_Q1'. The outcome of ' yPFLS_Q1=(sumEyimuA_Q1)/(summuA_Q1)' for some value is 'NAN', and I want to change 'nan' to zero. the structure of the code in .m file is correct but in Embedded function of Simulink does not answer.
for j=1:5
if j==1 && pmuA9 >=0 && pmuA8 >=0 && pmuA7 >=0 && pmuA6 >=0 && pmuA5 >=0 && pmuA4 >=0 && pmuA3 >=0 && pmuA2 >=0 && pmuA1 >=0
summuA_Q1=muA_9(j)+muA_8(j)+muA_7(j)+muA_6(j)+muA_5(j)+muA_4(j)+muA_3(j)+muA_2(j)+muA_1(j);
sumEyimuA_Q1=EyimuA_9(j)+EyimuA_8(j)+EyimuA_7(j)+EyimuA_6(j)+EyimuA_5(j)+EyimuA_4(j)+EyimuA_3(j)+EyimuA_2(j)+EyimuA_1(j);
yPFLS_Q1=(sumEyimuA_Q1)/(summuA_Q1);
if isnan(yPFLS_Q1)
yPFLS_Q1 = 0;
end
PyQ1=[PmuA_9(j) PmuA_8(j) PmuA_7(j) PmuA_6(j) PmuA_5(j) PmuA_4(j) PmuA_3(j) PmuA_2(j) PmuA_1(j)];
PyQ1nonZERO = nonzeros(PyQ1);
if isempty(PyQ1nonZERO)
PyPFLS_Q1 = 0;
else
PyPFLS_Q1 = max(PyQ1nonZERO);
end
end
end
It is not clear what the purpose of the loop is. You for j=1:5 but your if tests strictly j == 1 and there is no else for the cases where j is anything else. Therefore you might as well not use for j and instead just have j=1 and remove the j==1 from the test.
With the code only being valid for j == 1 then you should ask if it makes sense to index with j, since j is fixed: perhaps it makes more sense to put in literal 1's instead.
Can your PmuA_* variables ever be negative? If not then you can just max(PyQ1) instead of taking nonzeros() and max() that: if the result is 0 then there were no positive values, and if it is certain there are no negative values (hypothesis) then that would be enough to know.
It is confusing to see the same code have pmuA9 and PmuA_9 ? Because what I asked a moment ago about negative PmuA_* variables would be ruled out by the series of tests against pmuA* variables if they are the same variables as the PmuA_ variables.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by