Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a MATLAB function returns a variable number of values, how can I get all of them as a cell array?

I am writing a function to remove some values from a cell array, like so:

function left = remove(cells, item);
left = cells{cellfun(@(i) ~isequal(item, i), cells)};

But when I run this, left has only the first value, as the call to cells{} with a logical array returns all of the matching cells as separate values. How do I group these separate return values into a single cell array?

Also, perhaps there is already a way to remove a given item from a cell array? I could not find it in the documentation.

like image 202
prismofeverything Avatar asked May 12 '10 18:05

prismofeverything


People also ask

How do I count the number of values in an array in MATLAB?

n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .

How do you access the elements of a cell array in MATLAB?

There are two ways to refer to the elements of a cell array. Enclose indices in smooth parentheses, () , to refer to sets of cells--for example, to define a subset of the array. Enclose indices in curly braces, {} , to refer to the text, numbers, or other data within individual cells.

How do I return a cell array in MATLAB?

Description. C = cell( n ) returns an n -by- n cell array of empty matrices. C = cell( sz1,...,szN ) returns a sz1 -by-...

How do you count values in MATLAB?

A = count( str , pat ) returns the number of occurrences of pat in str . If pat is an array containing multiple patterns, then count returns the sum of the occurrences of all elements of pat in str . count matches elements of pat in order, from left to right.


1 Answers

You have to use () instead of {} to index the cells:

function left = remove(cells, item)
  left = cells(cellfun(@(i) ~isequal(item, i), cells));

Using () for indexing will give you a subset of cells, while using {} will return the contents of a subset of cells as a comma-separated list, and only the first entry of that list will get placed in left in your example.

You can check out this MATLAB documentation for more information on using cell arrays.


EDIT: Response to comment...

If you have an operation that ends up giving you a comma-separated list, you can place the individual elements of the list into cells of a cell array by surrounding the operation with curly braces. For your example, you could do:

left = {cells{cellfun(@(i) ~isequal(item, i), cells)}};

The inner set of curly braces creates a comma-separated list of the contents of cells that are not equal to item, and the outer set then collects this list into a cell array. This will, of course, give the same result as just using parentheses for the indexing, which is the more sensible approach in this case.

If you have a function that returns multiple output arguments, and you want to collect these multiple values into a cell array, then it's a bit more complicated. You first have to decide how many output arguments you will get, or you can use the function NARGOUT to get all possible outputs:

nOut = 3;                   %# Get the first three output arguments
%# Or...
nOut = nargout(@some_fcn);  %# Get all the output arguments from some_fcn

Then you can collect the outputs into a 1-by-nOut cell array outArgs by doing the following:

[outArgs{1:nOut}] = some_fcn(...);

It should be noted that NARGOUT will return a negative value if the function has a variable number of output arguments, so you will have to choose the value for nOut yourself in such a case.

like image 98
gnovice Avatar answered Oct 10 '22 09:10

gnovice