MATLAB: Combination function using ndgrid

combinationndgrid

Hello,
I want to build a function which use ndgrid.
My function is
function [ varargout ] = comb( varargin )
[varargout]=ndgrid(varargin)
varargout=varargout(:);
disp([varargout])
end
My varargout are x1 x2 x3 and varargin are a,b,c where a=[1 2], b=[2 3] and c=[1 4]. Expected results are
1 2 1
2 2 1
1 3 1
2 3 1
1 2 4
2 2 4
1 3 4
2 3 4
but it seems like ndgrid doesn't work. What can i do? In command window works fine but as a function where I can put more varargins something is wrong.

Best Answer

function varargout = comb(varargin)
varargout = cell(1, nargout);
[varargout{:}] = ndgrid(varargin{:}; %distribute to varargout
varargout = cellfun(@(m) reshape(m, [], 1), varargout, 'UniformOutput', false); %reshape into columns
end