Hi everyone!
I am stuck on the following problem: I would like to create a (class)function returning a variable number of output arguments, which can be used as input arguments for another function.
THe simplest example for this would be a function for indexing a multidimensional array.
For example, let's consider a 3dim array A:
A(1:2,1:2,1:2)=1;
A(:,:,2)=2;
>> A
A(:,:,1) =
1 1
1 1
A(:,:,2) =
2 2
2 2
I would like to have a function called Indexer(x,y,z) which I could use like this:
A(Indexer(1,1,2))
and would lead to the same result as using the following syntax:
>> A(1,1,2)
ans =
2
The problem is that I don't find a way to force the function Indexer to return 3 output arguments. A only takes the first argument returned by Indexer. And thus
>> A(Indexer(1,1,2))
ans =
1
Of course using this notation yields a correct result:
[x y z]=Indexer(1,1,2);
>> A(x,y,z)
ans =
2
Now, my approach was to somehow return a comma separated list to be used when indexing A, as this is the only way I now to parse multiple input/output arguments.
idx1={1,1,1};
idx2={1,1,2};
>> A(idx1{:})
ans =
1
>> A(idx2{:})
ans =
2
My idea was to utilize the subsref method in my own class, where I can manipulate the data as i want to, and then return a variable number of ouput arguments through a comma separeted list.
However, as I started to design the subsref function, I realized a strange thing happening in matlab, and I am realy stuck on this:
As I understand it, using the above notation, i.e. idx1{:}
calls matlab's builtin function subsref(idx1,s), where s is a struct with the following fields:
s.type='{}';
s.subs={':'};
but when I run the subsref function Matlab returns only the first argument, i.e:
>>idx1={1,1,1};
>>subsref(idx1,s)
ans =
1
Wheras when typing directly
>>idx1={1,1,1};
>>idx1{:}
ans =
1
ans =
1
ans =
1
I get the correct number of output arguments.
Does anyone know what is happening here. Am I missing something? Any help would be greatly appreciated.
Thx
Mohinda
