I am wondering if we can pass cell indexing as an input argument to a function, such that a cell inside the function can parse it.
For example, given a cell like x = num2cell(reshape(1:24, [2,3,4])), we can do indexing
>> x(:,1,2)
ans =
2×1 cell array
{[7]}
{[8]}
However, if I want to pass (:,1,2) as an input argument to a custom function (see the attempt below, which is not working for sure), what can we do?
f({:,1,2}) % not working
function y = f(varargin)
x = num2cell(reshape(1:24, [2,3,4]));
y = x(varargin{:});
end
vararginhere itself will be a cell. So if you go inside your functionfat runtime, you will see thatvararginis a cell that contains 1 element - another cell (this is what contains the inputs). When you then callx(varargin{:}), you are trying to use the inner cell as in index - hence the error. If you change this tof(2,1,2), it will now work. 2) the use of:. The colon operator cannot be directly passed if the context is not known. Hence,f(:,1,2)will not work. You can use the colon character':'instead -f(':',1,2)will work.