How can I create a variable of type "voidPtr" which points to a string using the LIBPOINTER function in MATLAB?
6 次查看(过去 30 天)
显示 更早的评论
I have a function in a generic library which takes a string as input. The function, however, expects a void pointer. When I try to call the function using the CALLLIB function with a string as input, I get the following error:
??? Error using ==> calllib
Array must be numeric or logical or a pointer to one.
采纳的回答
MathWorks Support Team
2011-12-5
This enhancement has been incorporated into the documentation as of MATLAB 7.2 (R2006a). For previous product releases, read below for any possible workarounds:
In C, characters are represented as unsigned 8-bit integers. In order to create a variable of type "voidPtr" containing a string, the string must first be cast to one of this numeric type as in the following code:
p=libpointer('voidPtr',[ uint8('this is a string') 0]);
The data in the pointer can then be accessed:
p.value
char(p.value)
To call a function that takes a voidPtr as input, use the following syntax:
foo(uint8(str));
where "foo" is the name of your function and "str" is the string.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call C from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!