mxCreateString usage when calling matlab

1 次查看(过去 30 天)
I try to call a function from mex in matlab and for this reason I made a mxArray because the function has two inputs namely a first one which is text and the second one which is a number.
My function works when I have static text, but not when I want to use the input to the mex-function. In the latter case I only get the first letter and not all 49 characters so I assume there is a problem with the pointers but I seriously cannot figure out were the problem lies
working code:
mxArray *callInput[2];
double inputNumber;
....
callInput[0] = mxCreateString("StringBeingPassed");
callInput[1] = mxCreateDoubleScalar(inputNumber);
mexCallMATLAB(0,NULL,2, callInput,"functionName");
NOT working anymore (only first letter being passed to function):
mxArray *callInput[2], *stringMX;
char *string_pr;
char string[49];
double inputNumber;
....
stringMX = mxGetField(prhs[0],0,"string");
string_pr = mxGetData(stringMX);
for (ii=0;ii<49;ii++){
*(yearString+ii) = *(yearString_pr+ii);
}
callInput[0] = mxCreateString(string);
callInput[1] = mxCreateDoubleScalar(inputNumber);
mexCallMATLAB(0,NULL,2, callInput,"functionName");

采纳的回答

James Tursa
James Tursa 2014-4-14
编辑:James Tursa 2014-4-14
MATLAB strings are stored internally as 2 bytes per character, not 1 byte per character. For regular ASCII text, that means that one of those bytes will be 0 (i.e., a null character). So in your for-loop you are actually only copying half the string, and every other byte you are copying is a null character. So the mxCreateString function only sees the first character (the 2nd being the first null character). Hence only 1 character gets copied in the mxArray you are creating. Some options:
1) Skip all the character-by-character copying and just use what you want, since it is already in mxArray form (recommended):
callInput[0] = stringMX;
2) Use an official API function to do this. E.g. (maybe you need the text as a C-string for some reason),
character *string;
string = mxArrayToString(stringMX);
callInput[0] = mxCreateString(string);
mxFree(string);
You can do the character-by-character copying manually, but you would have to copy every-other-character of the input and then tack on the null character yourself, ensuring that you don't run over the end of the input string during the copy (i.e., don't hard code a 49).
  1 个评论
Ingrid
Ingrid 2014-4-15
Thanks a lot! The first option already worked for me. I thought I had tried everything but not just using the stringMX (I was using mxArrayToString(stringMX) but of course now I see this simple solution I see were I was making a reasoning mistake).
After trying all possible combinations I tried to do the hardcopying to see if it would improve things but I had no idea about the two bytes per character stuff so I am glad that this forum exist so that I can learn new things.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by