| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
s = libstruct('structtype')
s = libstruct('structtype',mlstruct)
s = libstruct('structtype') returns a libstruct object s that is a MATLAB object designed to resemble a C structure of type specified by structtype. The structure type, structtype, is defined in an external library that must be loaded into MATLAB using the loadlibrary function.
Note Using this syntax, s is a NULL pointer. You, therefore, must ensure that any library function to which you pass s must be able to accept a NULL pointer as an argument. |
s = libstruct('structtype',mlstruct) returns a libstruct object s with its fields initialized from MATLAB structure, mlstruct.
The libstruct function creates a C-style structure that you can pass to functions in an external library. You handle this structure in MATLAB as you would a true MATLAB structure.
To determine which MATLAB types to use when passing arguments to library functions, see the output of libfunctionsview or libfunctions -full. These functions list all of the functions found in a particular library along with a specification of the types required for each argument.
This example performs a simple addition of the fields of a structure. The function addStructFields is defined in the MATLAB sample shared library, shrlibsample.
Here is the C function:
double addStructFields(struct c_struct st)
{
double t = st.p1 + st.p2 + st.p3;
return t;
}Start by loading the shrlibsample library and creating the structure, sm:
addpath([matlabroot '\extern\examples\shrlib']) loadlibrary shrlibsample shrlibsample.h sm.p1 = 476; sm.p2 = -299; sm.p3 = 1000;
Construct a libstruct object sc that uses the c_struct template:
sc = libstruct('c_struct', sm);
get(sc)
p1: 476
p2: -299
p3: 1000Now call the function, passing the libstruct object, sc:
calllib('shrlibsample', 'addStructFields', sc)
ans =
1177You must clear the libstruct object before unloading the library:
clear sc unloadlibrary shrlibsample
Note In most cases, you can pass a MATLAB structure and MATLAB automatically converts the argument to a C structure. See Working with Structures in the MATLAB External Interfaces documentation for more information. |
![]() | libpointer | license | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |