Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple output from function into cell array

Tags:

cell

matlab

I have a function with the following prototype

function [bandwidth,density,X,Y,x,y]=kde2d(data,n,MIN_XY,MAX_XY)

basically the function returns 6 outputs as above, some are in vector form while others are a numerical quantity. How can I elegantly pass the output from the function into a 1 by 6 cell array?

like image 464
gamerx Avatar asked Mar 20 '13 12:03

gamerx


1 Answers

how about

[a{1:6}] = kde2d( data, n, MIN_XY, MAX_XY )

Edit:

consider this annoying function

def foo(n):
  if n == 1:
    return [1, ]
  elif n == 2:
    return [1, ], {'a': 2}
  elif n == 3:
    return [1, ], {'a': 2}, (3, 3, 3)
  return [1, ], {'a': 2}, (3, 3, 3), None

You can always get all the outputs into a single tuple:

for i in range(1, 5):
  f = foo(i)
  print('got {} outputs: {}'.format(len(f), f))

and the output of this simple loop would be:

got 1 outputs: [1]
got 2 outputs: ([1], {'a': 2})
got 3 outputs: ([1], {'a': 2}, (3, 3, 3))
got 4 outputs: ([1], {'a': 2}, (3, 3, 3), None)

If you want to get a specific output:

f = foo(2)
f[1]   # accessing the second output, {'a': 2} in this example.
like image 111
Shai Avatar answered Oct 25 '22 09:10

Shai