MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   从C获取MATLAB变量(字符串) (https://www.labfans.com/bbs/showthread.php?t=23342)

poster 2019-12-10 20:41

从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文档,但是所有这些都使我明白了。



[B]回答:[/B]

您的问题是您试图将内存分配到从未分配过的内存中。 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);

[url=https://stackoverflow.com/questions/2690327]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 04:59

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.