"Expected a scalar" error and odd if-behavior
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!