Search code examples
matlabcellinline

Transform matlab function output (cell array) into comma separated list without temporary cell array


As by the title, how can a cell array that is the output of a matlab function be directly turned into a comma-separated list without using a temporary array?

I.e., I know you can write

% functioning code
tmp = cell(1,3); % function that makes a temporary cell_array;
b = ndgrid(tmp{:}); % transform tmp into a 
% comma-separated list and pass into another function

I am looking for a way that allows me to do this in a way like

% non functioning code
b = ndgrid( cell(1,3){:} );

so that it can be used within an anonymous function, where no temporary arguments are allowed. Example:

fun = @(x)accept_list( make_a_cell(x){:} );

How could this be achieved? I would think that there must be a function invoked when the operator '{:}' is used, but which one would it be?

EDIT for clarification:

The solution in the answer which this question was tagged to possibly be a duplicate of does not solve the problem, because subsref is not a replacement for {:} when creating a comma-separated list.

Example:

a = {1:2,3:4}
[A1,A2] = ndgrid(subsref(a, struct('type', '{}', 'subs', {{':'}})));

is (wrongly)

A1 =
     1     1
     2     2
A2 =
     1     2
     1     2

but

a = {1:2,3:4}    
[A1,A2] = ndgrid(a{:});

returns (correctly)

A1 =
     1     1
     2     2
A2 =
     3     4
     3     4

Solution

  • Ok, the answer is (see comments of Sardar Usama in the comments above) to replace

    fun = @(x)accept_list( make_a_cell(x){:} );
    

    by

    tmpfun = @(cell_arg, fun_needs_list)fun_needs_list( cell_arg{:} );
    fun = @(x)tmpfun(make_a_cell(x), accept_list);