calllib - Call function in C shared library - MATLAB (original) (raw)
Call function in C shared library
Syntax
Description
[[x1,...,xN](#btdfvoz-1-x1xN)] = calllib([libname](#btdfvoz-1%5Fsep%5Fshared-libname),[funcname](#btdfvoz-1-funcname),[arg1,...,argN](#btdfvoz-1-arg1argN))
calls function funcname
in C library libname
, passing input arguments arg1,...,argN
. Thecalllib
function returns output values obtained fromfuncname
in x1,...,xN
.
Examples
Call addStructByRef
Function
Load the library.
if ~libisloaded('shrlibsample') addpath(fullfile(matlabroot,'extern','examples','shrlib')) loadlibrary('shrlibsample') end
Display function signature.
libfunctionsview shrlibsample
[double, c_structPtr] addStructByRef(c_structPtr)
The input argument is a pointer to a c_struct
data type.
Create a MATLAB® structure, struct
:
struct.p1 = 4; struct.p2 = 7.3; struct.p3 = -290;
Call the function.
[res,st] = calllib('shrlibsample','addStructByRef',struct);
Display the results.
Cleanup.
unloadlibrary shrlibsample
Input Arguments
Name of shared library, specified as a character vector. Do not include the path or file extension in libname
.
If you call loadlibrary
using the alias
option, then you must use the alias name for the libname
argument.
Data Types: char
funcname
— Name of function in library
character vector
Name of function in library, specified as a character vector.
Data Types: char
arg1,...,argN
— Input arguments
any type
Input arguments, 1 through N, required by funcname
(if any), specified by any type. The funcname
argument list specifies the argument type.
Output Arguments
x1,...,xN
— Output arguments
any type
Output arguments, 1 through N, from funcname
(if any), returned as any type. The funcname
argument list specifies the argument type.
Limitations
- Use with libraries that are loaded using the
loadlibrary
function.
Tips
- MATLAB validates input argument types before callingfuncname. If MATLAB displays error messages about data types, check the MATLAB function signature. For example, if
funcname
is in librarymylib
type:
libfunctions('mylib','-full')
To findfuncname
, scroll through the output. For more information, refer to your library documentation.
When you callfuncname
, that function might display errors. For information about error messages, refer to your library documentation.
Version History
Introduced before R2006a