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 function convert double to string

Subject: mex function convert double to string

From: Nate

Date: 24 Aug, 2011 13:53:29

Message: 1 of 6

I am trying to convert a double of known length to a string using a mex function

Ideally, this is how the mex function would work,

num2str_mex(1)
ans =
  '1'

This should be fairly simple, but I'm not having a whole lot of luck.

Thanks for the help,
Nate

The actual code should look something like,

#include "mex.h"
void num2str_mex(char *output, double input)
{
    /* convert input of type double to output of type char */
    /* not sure what goes here */
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    double *input
    char *output
    /* allocate memory for output string */
    output = mxCalloc(1, sizeof(char));
    /* create string array for return argument */
    plhs[0] = mxCreateString(output);
    /* assign pointers to output */
    input = mxGetPr(prhs[0]);
    /* call num2str_mex subroutine */
    num2str_mex(output,input);
}

Subject: mex function convert double to string

From: Bruno Luong

Date: 24 Aug, 2011 14:37:28

Message: 2 of 6

Try this:

#include "mex.h"
#include <stdio.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    double *input;
    char *output;
    
    input = mxGetPr(prhs[0]);
    output = mxMalloc(25*sizeof(char));
    sprintf(output, "%f", *input);
    plhs[0] = mxCreateString(output);
    mxFree(output);
}

% Bruno

Subject: mex function convert double to string

From: Bruno Luong

Date: 24 Aug, 2011 14:49:28

Message: 3 of 6

Or if you don't want to bother with malloc but uses a stack memory.

#include "mex.h"
#include <stdio.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    char output[25];
    sprintf(output, "%g", *mxGetPr(prhs[0]));
    plhs[0] = mxCreateString(output);
}

% Bruno

Subject: mex function convert double to string

From: Nate

Date: 24 Aug, 2011 14:49:28

Message: 4 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j332b7$7qj$1@newscl01ah.mathworks.com>...
> Try this:
>
> #include "mex.h"
> #include <stdio.h>
>
> void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
> {
> double *input;
> char *output;
>
> input = mxGetPr(prhs[0]);
> output = mxMalloc(25*sizeof(char));
> sprintf(output, "%f", *input);
> plhs[0] = mxCreateString(output);
> mxFree(output);
> }
>
> % Bruno

Awesome, thank you.
Nate

Subject: mex function convert double to string

From: Steven_Lord

Date: 24 Aug, 2011 15:26:41

Message: 5 of 6



"Nate " <natejensen88@gmail.com> wrote in message
news:j32vop$s22$1@newscl01ah.mathworks.com...
> I am trying to convert a double of known length to a string using a mex
> function

Could you explain why you need/want to do this in or using a MEX-file? I
would just call one of the MATLAB functions NUM2STR or SPRINTF unless the
MEX-ness is important.

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

Subject: mex function convert double to string

From: Nate

Date: 24 Aug, 2011 16:24:25

Message: 6 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <j3357h$hsk$1@newscl01ah.mathworks.com>...
>
>
> "Nate " <natejensen88@gmail.com> wrote in message
> news:j32vop$s22$1@newscl01ah.mathworks.com...
> > I am trying to convert a double of known length to a string using a mex
> > function
>
> Could you explain why you need/want to do this in or using a MEX-file? I
> would just call one of the MATLAB functions NUM2STR or SPRINTF unless the
> MEX-ness is important.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I am trying to write a function that converts serial dates to readable date strings, kind of like Jan Simon's DateStr2Num found here,
http://www.mathworks.com/matlabcentral/fileexchange/28093-datestr2num
except for the opposite way.

Currently I am reading in Unix time (POSIX time) and converting that serial date to representative numbers. For example, I am able to turn,
1314192660
into
2011 (year)
8 (month)
24 (day)
9 (hour)
31 (minute)
0 (second)
and now I would like to turn those numbers into a string,
'08/24/2011 09:31:00'

I have written a Matlab version of this code, but I wanted to write a mex version because I was hoping that it might go faster.

Thanks again for the help,
Nate

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