"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. Nonscalars 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).
