Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
mex gateway function

Subject: mex gateway function

From: Michal Kvasnicka

Date: 3 Jan, 2013 12:50:08

Message: 1 of 6

How to create suitable mex gateway function coresponduing to the following C function (decimal to binary conversion)?

  void deci2bin( int x, int n, double* output){
  double *temp;
  int i;
  temp = (double *)mxMalloc(sizeof(double)*n);
  i=0;
  while(x>=0 && i<n){
    temp[i] = x%2;
    i++;
  }
  for(i=0;i<n;i++)
    output[i] = temp[n-1-i];
  }

Thanks in advance for any help.

Subject: mex gateway function

From: Bruno Luong

Date: 3 Jan, 2013 13:12:08

Message: 2 of 6

"Michal Kvasnicka" wrote in message <kc3uq0$t49$1@newscl01ah.mathworks.com>...
> How to create suitable mex gateway function coresponduing to the following C function (decimal to binary conversion)?
>
> void deci2bin( int x, int n, double* output){
> double *temp;
> int i;
> temp = (double *)mxMalloc(sizeof(double)*n);
> i=0;
> while(x>=0 && i<n){
> temp[i] = x%2;
> i++;
> }
> for(i=0;i<n;i++)
> output[i] = temp[n-1-i];
> }
>
> Thanks in advance for any help.

The C-gateway is the same for all MATLAB function, regardless how the intended usage.
  
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
...
}

The doc for external interface explains all that, and no user can afford to skip the reading.

Bruno

Subject: mex gateway function

From: Steven_Lord

Date: 3 Jan, 2013 14:32:23

Message: 3 of 6



"Michal Kvasnicka" <michal.kvasnicka@gmail.remove.com> wrote in message
news:kc3uq0$t49$1@newscl01ah.mathworks.com...
> How to create suitable mex gateway function coresponduing to the following
> C function (decimal to binary conversion)?

I probably wouldn't -- I'd use the MATLAB function DEC2BIN instead.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: mex gateway function

From: Bruno Luong

Date: 3 Jan, 2013 14:54:09

Message: 4 of 6

>
> I probably wouldn't -- I'd use the MATLAB function DEC2BIN instead.

Last time I check, DEC2BIN and friends are pretty slow and they are worth to be mexed if speed is critical.

Bruno

Subject: mex gateway function

From: Michal Kvasnicka

Date: 3 Jan, 2013 20:15:06

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kc462g$sfr$1@newscl01ah.mathworks.com>...
> >
> > I probably wouldn't -- I'd use the MATLAB function DEC2BIN instead.
>
> Last time I check, DEC2BIN and friends are pretty slow and they are worth to be mexed if speed is critical.
>
> Bruno
Yes, I am looking for fast (mexed) decimal to binary conversion utility!!!

Subject: mex gateway function

From: James Tursa

Date: 3 Jan, 2013 23:02:08

Message: 6 of 6

"Michal Kvasnicka" wrote in message <kc3uq0$t49$1@newscl01ah.mathworks.com>...
> How to create suitable mex gateway function coresponduing to the following C function (decimal to binary conversion)?
>
> void deci2bin( int x, int n, double* output){
> double *temp;
> int i;
> temp = (double *)mxMalloc(sizeof(double)*n);
> i=0;
> while(x>=0 && i<n){
> temp[i] = x%2;
> i++;
> }
> for(i=0;i<n;i++)
> output[i] = temp[n-1-i];
> }

In addition to looking for a fast utility, you should probably be looking for a correct one as well. I don't see how the above routine can work at all since x is not altered during the while loop extracting the "bits" via the x%2 operation. Also, what is supposed to happen for negative x inputs? In the above code, you will end up copying unitialized memory into output for this case. Bottom line is the above code has bugs that need fixing.

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us