From: Praetorian on
On Jan 18, 9:27 am, "George Coles" <geo...(a)quantitative.com> wrote:
> Loren Shure <loren.sh...(a)mathworks.com> wrote in message <MPG.25be2afb1f703587989...(a)news.mathworks.com>...
> > In article <hisld7$so...(a)fred.mathworks.com>, geo...(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

The nargout syntax seems to work for me. Here are the files I created
to test this:

-------------- mexfile ------------------------

#include "mex.h"

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[])
{
mexPrintf( "nlhs = %d\n", nlhs );

for( int i = 0; i < nlhs; i++ ) {
plhs[i] = mxCreateDoubleMatrix( 0, 0, mxREAL );
}
}

------------ end mexfile -------------------

------------ mfile -----------------------

function varargout = mtest

[varargout{1:nargout}] = mextest;

------------ end mfile -----------------

Now call mtest as follows:

[a b c d] = testmfile
nlhs = 4
a =
[]
b =
[]
c =
[]
d =
[]


Is this what you were looking for?

HTH,
Ashish.
From: Praetorian on
On Jan 18, 10:08 am, Praetorian <ashish.sadanan...(a)gmail.com> wrote:
> On Jan 18, 9:27 am, "George Coles" <geo...(a)quantitative.com> wrote:
>
>
>
> > Loren Shure <loren.sh...(a)mathworks.com> wrote in message <MPG.25be2afb1f703587989...(a)news.mathworks.com>...
> > > In article <hisld7$so...(a)fred.mathworks.com>, geo...(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
>
> The nargout syntax seems to work for me. Here are the files I created
> to test this:
>
> -------------- mexfile ------------------------
>
> #include "mex.h"
>
> void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray
> *prhs[])
> {
>   mexPrintf( "nlhs = %d\n", nlhs );
>
>   for( int i = 0; i < nlhs; i++ ) {
>     plhs[i] = mxCreateDoubleMatrix( 0, 0, mxREAL );
>   }
>
> }
>
> ------------ end mexfile -------------------
>
> ------------ mfile -----------------------
>
> function varargout = mtest
>
> [varargout{1:nargout}] = mextest;
>
> ------------ end mfile -----------------
>
> Now call mtest as follows:
>
> [a b c d] = testmfile
> nlhs = 4
> a =
>      []
> b =
>      []
> c =
>      []
> d =
>      []
>
> Is this what you were looking for?
>
> HTH,
> Ashish.

I screwed up the m file name in the call example because I renamed the
file before I posted:

[a b c d] = mtest
nlhs = 4
a =
[]
b =
[]
c =
[]
d =
[]
First  |  Prev  | 
Pages: 1 2
Prev: Permission Denied using save -append
Next: reshape