From: Adam Hartshorne on
Say I have a function X, that takes an input Y and returns an array / matrix. Now I really only want the nth element of the array / matrix.

Obviously I could do,

temp = X(Y)
wantedVar = temp(n) ;

Is there an easy way of doing both operations in a single line of code, something along the lines of

wantedVar = (X(Y))(n)

Any help much appreciated,

Adam
From: Doug Schwarz on
In article <hmkkoo$phj$1(a)fred.mathworks.com>,
"Adam Hartshorne" <adam.hartshorne(a)gmail.com> wrote:

> Say I have a function X, that takes an input Y and returns an array / matrix.
> Now I really only want the nth element of the array / matrix.
>
> Obviously I could do,
>
> temp = X(Y)
> wantedVar = temp(n) ;
>
> Is there an easy way of doing both operations in a single line of code,
> something along the lines of
>
> wantedVar = (X(Y))(n)
>
> Any help much appreciated,
>
> Adam

It's a FAQ and the answer is no.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
From: Matt J on
"Adam Hartshorne" <adam.hartshorne(a)gmail.com> wrote in message <hmkkoo$phj$1(a)fred.mathworks.com>...
> Say I have a function X, that takes an input Y and returns an array / matrix. Now I really only want the nth element of the array / matrix.
>
> Obviously I could do,
>
> temp = X(Y)
> wantedVar = temp(n) ;
>
> Is there an easy way of doing both operations in a single line of code, something along the lines of
>
> wantedVar = (X(Y))(n)
=====================

If you're only looking for the benefits of simplified syntax, this will let you do something very similar:

http://www.mathworks.com/matlabcentral/fileexchange/26570-direct-indexing-of-function-calls-oop-exercise

Note that if your function X is not a native MATLAB function, you can add a handle to it in the LibraryStruct.mat structure, S. You could also make S the constant property of some class so that it is globally accessible.