How can I create a variable of type "voidPtr" which points to a string using the LIBPOINTER function in MATLAB?

6 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Dec 2011
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.

More Answers (0)

Products


Release

R14SP2

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!