Prev: Permission Denied using save -append
Next: reshape
From: George Coles on 16 Jan 2010 10:19 I have tried to find some documentation regarding using the varargout mechanism with mex functions. Perhaps I did not look hard enough, but I have not seen any discussion of why this seems not to work. I wrote a simple test function that creates the same number of dummy outputs as the value in the nrhs argument in mexFunction(). If I call [a,b,c] = test(1,2,3), setting the output arguments manually, it works. If I use varargout{1:nargout} = test(1,2,3) nargout always seems to be equal to 1. Does anyone know if this should be supported, and if not, does anyone know of a workaround? thx George
From: Doug Schwarz on 16 Jan 2010 10:28 In article <hisld7$sou$1(a)fred.mathworks.com>, "George Coles" <george(a)quantitative.com> wrote: > I have tried to find some documentation regarding using the varargout > mechanism with mex functions. Perhaps I did not look hard enough, but I have > not seen any discussion of why this seems not to work. I wrote a simple test > function that creates the same number of dummy outputs as the value in the > nrhs argument in mexFunction(). If I call > > [a,b,c] = test(1,2,3), > > setting the output arguments manually, it works. If I use > > varargout{1:nargout} = test(1,2,3) > > nargout always seems to be equal to 1. Does anyone know if this should be > supported, and if not, does anyone know of a workaround? > > thx > George You should use varargout and nargout inside an m-file (where nargout knows how many output arguments you specified). To get what I think you want try c = cell(1,3); [c{:}] = test(1,2,3); -- Doug Schwarz dmschwarz&ieee,org Make obvious changes to get real email address.
From: Loren Shure on 18 Jan 2010 08:23 In article <hisld7$sou$1(a)fred.mathworks.com>, george(a)quantitative.com says... > I have tried to find some documentation regarding using the varargout mechanism with mex functions. Perhaps I did not look hard enough, but I have not seen any discussion of why this seems not to work. I wrote a simple test function that creates the same number of dummy outputs as the value in the nrhs argument in mexFunction(). If I call > > [a,b,c] = test(1,2,3), > > setting the output arguments manually, it works. If I use > > varargout{1:nargout} = test(1,2,3) > > nargout always seems to be equal to 1. Does anyone know if this should be supported, and if not, does anyone know of a workaround? > > thx > George > The latter is correct as is. varargout{1:3} produces a length 3 comma- separated list, not a list of outputs as in your first syntax. As a result, test(1,2,3) only sees one output (varargout{1}). -- Loren http://blogs.mathworks.com/loren
From: George Coles on 18 Jan 2010 11:27 Loren Shure <loren.shure(a)mathworks.com> wrote in message <MPG.25be2afb1f703587989aa2(a)news.mathworks.com>... > In article <hisld7$sou$1(a)fred.mathworks.com>, george(a)quantitative.com > says... > > I have tried to find some documentation regarding using the varargout mechanism with mex functions. Perhaps I did not look hard enough, but I have not seen any discussion of why this seems not to work. I wrote a simple test function that creates the same number of dummy outputs as the value in the nrhs argument in mexFunction(). If I call > > > > [a,b,c] = test(1,2,3), > > > > setting the output arguments manually, it works. If I use > > > > varargout{1:nargout} = test(1,2,3) > > > > nargout always seems to be equal to 1. Does anyone know if this should be supported, and if not, does anyone know of a workaround? > > > > thx > > George > > > > The latter is correct as is. varargout{1:3} produces a length 3 comma- > separated list, not a list of outputs as in your first syntax. As a > result, test(1,2,3) only sees one output (varargout{1}). > > -- > Loren > http://blogs.mathworks.com/loren My situation is that I have a mex function written in c++ that takes a string argument, which is a query submitted to a specialized database API. The query could return any number of columns, each of which is returned from the mex function as a cell array. If the number of columns returned is known, or guessed, it is straightforward to hard-code the size of the cell array that holds all the outputs. What I am trying to support is the scenario where the number of return values is not known in advance. I would like to detect the number of output arguments that have been placed into plhs[0] ... plhs[n]. Does this clarify my earlier question? Is it possible? My workaround now is to parse the query string and discover the number of columns to expect but it would be more robust to use built-in Matlab features to accomplish the same goal. thx g
From: James Tursa on 18 Jan 2010 11:38
"George Coles" <george(a)quantitative.com> wrote in message <hj224m$lcb$1(a)fred.mathworks.com>... > > My situation is that I have a mex function written in c++ that takes a string argument, which is a query submitted to a specialized database API. The query could return any number of columns, each of which is returned from the mex function as a cell array. If the number of columns returned is known, or guessed, it is straightforward to hard-code the size of the cell array that holds all the outputs. What I am trying to support is the scenario where the number of return values is not known in advance. > I would like to detect the number of output arguments that have been placed into > plhs[0] ... plhs[n]. > Does this clarify my earlier question? Is it possible? My workaround now is to parse the query string and discover the number of columns to expect but it would be more robust to use built-in Matlab features to accomplish the same goal. The best solution is probably to return everything in one cell array. Just make one plhs[0] return argument and put all of the stuff you have been putting into plhs[0] ... plhs[n] and make them cells of plhs[0] instead. Then on the MATLAB side you will have one output and can pull it apart there. James Tursa |