"Expected a scalar" error and odd if-behavior
35 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm really confused with "Expected a scalar" error in simulink because I'm (almost) 100% sure that the values are indeed [1x1] scalars. Following matlab function is a small part of my simulink model with several inputs and outputs. I have extracted the function from the model and changed inputs to constant-blocks, but the error still occurs.
inputs check, P, hi and lo are constants with values as seen in figure above, arrays a, array1 and array have sizes [30801x7], [2531x5] and [12655x1], respectively.
Heres the function code:
function [res1,res2,res3,res4,state]= fcn(lo,hi,a,P,array1,array,check)
%#codegen
[r c] = find(array1==P);
if check == 1
B = [a(lo+1:P+1,1),a(lo+1:P+1,3),a(lo+1:P+1,5),a(lo+1:P+1,7)];
C = [a(P+2:hi+1,1),a(P+2:hi+1,2),a(P+2:hi+1,4),a(P+2:hi+1,6)];
A = [B;C];
elseif check == 2
i = array1(1,c);
B = [a(lo+1:i,1),a(lo+1:i,3),a(lo+1:i,5),a(lo+1:i,7)];
C = [a(i+1:hi+1,1),a(i+1:hi+1,2),a(i+1:hi+1,4),a(i+1:hi+1,6)];
A = [B;C];
elseif check == 4
max_val = max(array1);
i = max_val(c);
B = [a(lo+1:22770,1),a(lo+1:22770,3),a(lo+1:22770,5),a(lo+1:22770,7)];
C = [a(22770+1:hi+1,1),a(22770+1:hi+1,2),a(22770+1:hi+1,4),a(22770+1:hi+1,6)];
A = [B;C];
else
B = [a(lo+1:P,1),a(lo+1:P,3),a(lo+1:P,5),a(lo+1:P,7)];
C = [a(P+1:hi+1,1),a(P+1:hi+1,2),a(P+1:hi+1,4),a(P+1:hi+1,6)];
A = [B;C];
end
[res1, i] = min(A(:,3));
res3 = A(i,2);
res4 = A(i,4);
res2 = A(i,1);
if (res2 < P) && (any(res2==array) == 0)
state = 1;
elseif (res2 < P) && (any(res2==array) == 1)
state = 2;
elseif (res2 >= P) && (any(res2==array) == 0)
state = 3;
else
state = 4;
end
and few example error codes:
Expected a scalar value. This expression has size [1 x :?].
Function 'MATLAB Function1' (#35.326.327), line 11, column 17:
"i"
Launch diagnostic report.
---------------------------------------------
Expected a scalar. Non-scalars are not supported in IF or WHILE statements, or with logical operators.
Instead, use ALL to convert matrix logicals to their scalar equivalents.
Function 'MATLAB Function1' (#35.915.923), line 31, column 5:
"res2 < P"
Launch diagnostic report.
Why do I get these scalar-errors? If I run this in matlab line by line and try these variables with isscalar, I get boolean value 1 (meaning they are in fact scalars)
Also, why do I get the first error about i not being a scalar after elseif check == 2? check has value 1 and therefore should even go inside that elseif operator.
0 个评论
采纳的回答
Walter Roberson
2016-3-21
The code has no reason to know that
[r c] = find(array1==P);
is going to find exactly 1 value, not 0 values and not 2 or more values. Therefore r and c are being created as variable dimension. Then you have
i = array1(1,c);
with c being variable dimension, i is going to have to be variable dimension. So when you have
res2 = A(i,1);
then res2 is going to have to be variable dimension. When you then compare res2 < P that is a variable dimension, which is not permitted in an "if".
You would probably point out that, "But
[res1, i] = min(A(:,3));
and min() always returns a single index, so i is going to be a scalar, not a vector!"
The problem with that is that it ignores that for code generation, the size and data type of a variable is always determined by the first assignment to the variable. You assigned a variable dimension to it first, so it is going to remain variable dimension.
Hint: if you are sure that exactly one value is going to be returned, then
[r_ c_] = find(array1==P);
r = r_(1); c = c_(1);
and now the code knows for sure that r and c are scalars.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!