From: James Pau on 17 Jul 2010 01:18 Hi all, I'm having trouble displaying the value of string variables created in a c++ mex file. A sample situation is shown below: [code] #include "mex.h" #include <iostream> #include <string.h> using namespace std; void mexFunction( int nlhs, mxArray *[], int nrhs, const mxArray *prhs[] ) { string a; a = "test"; mexPrintf("%s \n",a); mexPrintf("%s \n","test"); } [\code] The output is: r test What this means is that the string variable 'a' is not being read correctly by the mexPrintf function and I have no idea why. Any thoughts would be greatly appreciated, I'm completely stuck! TIA, James
From: Jan Simon on 17 Jul 2010 07:16 Dear James, > string a; > a = "test"; > mexPrintf("%s \n",a); > mexPrintf("%s \n","test"); > > The output is: > r > test Not surprising. Look in the documentation of mexPrintf (or printf) and you find out, that the "%s" format displays character arrays and takes the pointer to the first character of the string. "string a" is not a character array, but a "string". Do you find a conversion from a "string" to a char vector? Good luck, Jan
From: James Pau on 17 Jul 2010 08:45 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i1s3e0$e6n$1(a)fred.mathworks.com>... > Dear James, > > > string a; > > a = "test"; > > mexPrintf("%s \n",a); > > mexPrintf("%s \n","test"); > > > > The output is: > > r > > test > > Not surprising. Look in the documentation of mexPrintf (or printf) and you find out, that the "%s" format displays character arrays and takes the pointer to the first character of the string. "string a" is not a character array, but a "string". Do you find a conversion from a "string" to a char vector? > > Good luck, Jan Hi Jan, Thank you for your response. I did read the documentation for printf before and sort of assumed that a character array was equivalent to a string. My mistake, but I have managed to get it to work. For those who are interested the amended code is as follows: string a; char b[260]; a = "test"; strcpy(b,a.c_str()); mexPrintf("%s \n",b); Output for b will correctly be "test'. Thanks again! James
From: Jan Simon on 17 Jul 2010 11:53 Dear James, > strcpy(b,a.c_str()); > mexPrintf("%s \n",b); > Output for b will correctly be "test'. Thanks. This is the way a newsgroup takes advantage from the solved questions of users. You are my personal poster of the week! Jan
From: Praetorian on 17 Jul 2010 19:04 On Jul 17, 6:45 am, "James Pau" <the_manti...(a)hotmail.com> wrote: > "Jan Simon" <matlab.THIS_Y...(a)nMINUSsimon.de> wrote in message <i1s3e0$e6....(a)fred.mathworks.com>... > > Dear James, > > > > string a; > > > a = "test"; > > > mexPrintf("%s \n",a); > > > mexPrintf("%s \n","test"); > > > > The output is: > > > r > > > test > > > Not surprising. Look in the documentation of mexPrintf (or printf) and you find out, that the "%s" format displays character arrays and takes the pointer to the first character of the string. "string a" is not a character array, but a "string". Do you find a conversion from a "string" to a char vector? > > > Good luck, Jan > > Hi Jan, > > Thank you for your response. I did read the documentation for printf before and sort of assumed that a character array was equivalent to a string. My mistake, but I have managed to get it to work. For those who are interested the amended code is as follows: > > string a; > char b[260]; > a = "test"; > strcpy(b,a.c_str()); > mexPrintf("%s \n",b); > > Output for b will correctly be "test'. > > Thanks again! > James James, You shouldn't have to perform the copy to variable b either. The string::c_str() function returns a const char * to the character array so mexPrintf() should be able to print it. If mexPrintf() has a problem with it being a const pointer a const_cast should be able to fix that. HTH, Ashish.
|
Pages: 1 Prev: clearing current folders list Next: Image Processing, Skeleton questions |