Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
variable problem in MATLAB function

Subject: variable problem in MATLAB function

From: Firat

Date: 27 Feb, 2012 20:14:12

Message: 1 of 4

Hi all,
I am using a MATLAB function block in my Simulink model.

In this block, I use an if statement like the following in a portion:

function
index = find(max(Jdiag)==Jdiag) ;

if index ==4
    p(1)=(J(3,2)-J(2,3))/p(4);
    p(2)=(J(1,3)-J(3,1))/p(4);
    p(3)=(J(2,1)-J(1,2))/p(4);
end


When I start the simulation, it gives an error saying that :
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 'x' (#396.836.845), line 29, column 4:
"index ==4"

I know that the index variable is not an array. I tried it in a script and it works but in MATLAB function I could not get the code to work. Is "find" the problem ?

Thanks for your help,
Firat

Subject: variable problem in MATLAB function

From: Firat

Date: 27 Feb, 2012 20:46:11

Message: 2 of 4

isequal(index,4) did the trick

"Firat" wrote in message <jigo6k$64a$1@newscl01ah.mathworks.com>...
> Hi all,
> I am using a MATLAB function block in my Simulink model.
>
> In this block, I use an if statement like the following in a portion:
>
> function
> index = find(max(Jdiag)==Jdiag) ;
>
> if index ==4
> p(1)=(J(3,2)-J(2,3))/p(4);
> p(2)=(J(1,3)-J(3,1))/p(4);
> p(3)=(J(2,1)-J(1,2))/p(4);
> end
>
>
> When I start the simulation, it gives an error saying that :
> 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 'x' (#396.836.845), line 29, column 4:
> "index ==4"
>
> I know that the index variable is not an array. I tried it in a script and it works but in MATLAB function I could not get the code to work. Is "find" the problem ?
>
> Thanks for your help,
> Firat

Subject: variable problem in MATLAB function

From: someone

Date: 27 Feb, 2012 21:14:13

Message: 3 of 4

"Firat" wrote in message <jigq2j$cpo$1@newscl01ah.mathworks.com>...
> isequal(index,4) did the trick
>
> "Firat" wrote in message <jigo6k$64a$1@newscl01ah.mathworks.com>...
> > Hi all,
> > I am using a MATLAB function block in my Simulink model.
> >
> > In this block, I use an if statement like the following in a portion:
> >
> > function
> > index = find(max(Jdiag)==Jdiag) ;
>
> > if index ==4
> > p(1)=(J(3,2)-J(2,3))/p(4);
> > p(2)=(J(1,3)-J(3,1))/p(4);
> > p(3)=(J(2,1)-J(1,2))/p(4);
> > end
> >
> >
> > When I start the simulation, it gives an error saying that :
> > 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 'x' (#396.836.845), line 29, column 4:
> > "index ==4"
> >
> > I know that the index variable is not an array. I tried it in a script and it works but in MATLAB function I could not get the code to work. Is "find" the problem ?
> >
> > Thanks for your help,
> > Firat

isequal(index,4) may have inhibited the error message, but your code
may not be giving you the results you expect.
From the help documentation for find:
ind = find(X) locates *all* nonzero elements of array X, and returns the linear
indices of those elements in *vector* ind. (My emphasis on all and vector.)
I strongly suspect you have (at least) two maximums in Jdiag.
To test, simply remove the semicolon from:
index = find(max(Jdiag)==Jdiag) ;
and see what index is.
The other less likely possibility is that index is empty.
The isequal would "fix" both of these cases (but maybe not the way you expect).

Subject: variable problem in MATLAB function

From: Murat Ates

Date: 12 Mar, 2012 08:41:11

Message: 4 of 4

Hi Folks,

I have a similar problem with "find" function. I am 100% sure it gives me only one index number.

*********************************

gear = find(maxSpeed < actualSpeed, 1, 'last') + 1;
if isempty(gear)
    gear = 1;
end

**********************************

This code does not give me a problem if I don't use "gear" variable in the remaining code. There is an if statement that contains "gear" throws the following error:

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.

It seems to me Simulink understands there will be some empty gear variables to be in some time-steps and throws that error although I check for that in next line.

Thanks,
Murat

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us