poster
2019-12-10, 20:41
我正在编写一个启动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文档,但是所有这些都使我明白了。
回答:
您的问题是您试图将内存分配到从未分配过的内存中。 char * varx = malloc(sizeof(char)* bytes_you_need);在您这样做之前。将char *设置为NULL意味着它没有内存地址,因此不能用作对任何内存的引用。...将其设置为malloc的返回值,其中malloc已为数据预留了一些字节。
char *varx = malloc (sizeof(char) * n); memcpy(varx, mxGetData(result),n*sizeof(char)); printf ("%s\n", varx); free(varx);
更多&回答... (https://stackoverflow.com/questions/2690327)
我正在做这样的事情:
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文档,但是所有这些都使我明白了。
回答:
您的问题是您试图将内存分配到从未分配过的内存中。 char * varx = malloc(sizeof(char)* bytes_you_need);在您这样做之前。将char *设置为NULL意味着它没有内存地址,因此不能用作对任何内存的引用。...将其设置为malloc的返回值,其中malloc已为数据预留了一些字节。
char *varx = malloc (sizeof(char) * n); memcpy(varx, mxGetData(result),n*sizeof(char)); printf ("%s\n", varx); free(varx);
更多&回答... (https://stackoverflow.com/questions/2690327)