Const char into mex files

5 ビュー (過去 30 日間)
Jorge Mario Guerra González
Jorge Mario Guerra González 2016 年 9 月 12 日
コメント済み: James Tursa 2016 年 9 月 14 日
Hello,
I'd like to know how can I export my Matlab strings to use them whitin a MEX file, as it follows. I'm quite new using MEX files and I'd really appreciate any help
I have in matlab this variable
folder = 'C:\Users\Jorgue Guerra\Desktop\Sample_1'
then I want to export that value to use in C, I've tried something like this.
void mexFunction(int nlhs, mxArray *plhs[], //output
int nrhs, const mxArray *prhs[]){ //input
const char *folder= (const char)mxGetData(prhs[0])
//.....other code using folder var
}
I know it can't be as simple as that, but I have no clue where to begin
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 9 月 13 日
At the very least it would have to be
const char *folder= (const char *)mxGetData(prhs[0]);
because mxGetData returns a pointer, not a character.
James Tursa
James Tursa 2016 年 9 月 14 日
編集済み: James Tursa 2016 年 9 月 14 日
Even this won't work, because MATLAB char data is 2-bytes per character, whereas C char data is 1-byte per character. So you need something like the mxArrayToString API function to do an actual allocation + copy. Or one could use this to access the MATLAB char data manually:
const unsigned short *folder= (const unsigned short *)mxGetData(prhs[0]);

サインインしてコメントする。

採用された回答

Jorge Mario Guerra González
Jorge Mario Guerra González 2016 年 9 月 13 日
I solved it by myself.
If anyone is in trouble with this the answer is given in this page
  1 件のコメント
James Tursa
James Tursa 2016 年 9 月 14 日
FYI, that link is a bit misleading. The memory for output_buf does not become part of the plhs[0] output as a result of the mxCreateString call, so it should in fact be released just like the memory for input_buf is released. I.e., this line should appear at the end of the code:
mxFree(output_buf);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite 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