从 C 获取 MATLAB 变量(字符串)
我正在编写一个启动 Matlab 脚本(.m 文件)的小型 C 应用程序。 我需要交换一些变量,但我不知道如何获取 Matlab 中存在的字符数组。
我正在做这样的事情:
enter code here
result = engGetVariable(ep,"X");
if (!result)
{
printf ("Error...");
exit -1;
}
int n = mxGetN(result);
char *varx = NULL;
memcpy(varx, mxGetData(result),n*sizeof(char));
它不起作用。有人知道如何用 C 语言获取 Matlab 字符串吗? 我已经阅读了有关 engGetVariable() 的 Matlab 文档和提供的示例,但这些都让我明白了。
I'm writing a small C application that launchs a Matlab script (.m file).
I need to exchange some variables and I don't know how to get an array of chars that exists in Matlab.
I'm doing something like this:
enter code here
result = engGetVariable(ep,"X");
if (!result)
{
printf ("Error...");
exit -1;
}
int n = mxGetN(result);
char *varx = NULL;
memcpy(varx, mxGetData(result),n*sizeof(char));
It doesn't work. Does someone know how to get a Matlab string in C?
I've read Matlab documentation about engGetVariable() and the provided example but any of this things clarify me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是你试图 memcpy 到你从未分配的内存中。
char *varx = malloc (sizeof(char) *bytes_you_need);在你这样做之前。将 char * 设置为 NULL 意味着它没有内存地址,因此不能作为对任何内存的引用......将其设置为 malloc 的返回值,其中 malloc 为您的数据留出了一些字节。
Your problem is that you're trying to memcpy into memory that you never allocated.
char *varx = malloc (sizeof(char) *bytes_you_need); before you do that. Setting char * to NULL means it has no memory address, and thus cannot serve as a reference to any memory.... set it to the return value of malloc, where malloc has set aside some bytes for your data.