pointing to an element in a returned function

I'm not certain how to describe this question, so my title may have not been as descriptive as I would have liked. To start off, I know my way around matlab fairly well, so you are welcome to respond accordingly. Lets say that I have a function that returns a 1x5 array. ie.
x=Calc() % returns x=[1 4 2 6 4]
now I want to use the 4th element in x (x(4)=6)to perform a calculation, such as
y=x*10; % which returns y=60
is there a way to perform this calculation without defining x by pointing to the fourth element that would be returned by Calc()? as in
y=Calc().{element4}*10;
another use would be to say that I have a matrix x, and I want to return element (4,2) from the product of x and another variable. y=x*15{element (4,1)} % obviously I could say x(4,2)*15, but that's not the point.
Thanks Jason

Answers (2)

There is no built-in method in MATLAB to do this.
You might want to add your vote / opinion over here
Work-around:
indexat = @(expression,varargin) expression(varargin{:});
and likewise
indexcellat = @(expression,varargin) expression{varargin{:}};
then you could use
y = indexat(Calc(),4);
and
y = indexat(x*15,4,1);
The basic answer is "no" - you need the intermediate variable.
There is some discussion of this issue on the newsgroup here. It's not a simple matter.
I have a feeling there was also a more recent discussion in Answers, but I can't find it now. Anyway, such discussions concern whether extending MATLAB to allow syntax such as Calc(x)(4) would be possible, practical or desirable in the future. For the present, you can't do it except in some special cases such as indexing into an element of a cell array.

1 Comment

Ah - I think Walter's pointed to the discussion I couldn't find.

Sign in to comment.

Asked:

on 15 Sep 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!