The function in the library multiplies the input by .
The input is a pointer to a , and the function returns a pointer to a . The MATLAB® function signature is:
Pass Pointer of Type double
This example shows how to construct and pass a pointer to C function .
Load the library containing the function.
Construct a pointer, , to the input argument, .
Verify the contents of .
Call the function and check the results.
is a handle object. Copies of this handle refer to the same underlying object and any operations you perform on a handle object affect all copies of that object. However, is not a C language pointer. Although it points to , it does not contain the address of . The function modifies the Value property of but does not modify the value in the underlying object . The original value of is unchanged.
Create Pointer Offset from Existing lib.pointer Object
This example shows how to create a pointer to a subset of a MATLAB vector . The new pointer is valid only as long as the original pointer exists.
Create a pointer to a vector.
Use the lib.pointer plus operator () to create a pointer to the last six elements of .
Multilevel pointers are arguments that have more than one level of referencing. A multilevel pointer type in MATLAB uses the suffix . For example, use for the C argument .
When calling a function that takes a multilevel pointer argument, use a object and let MATLAB convert it to the multilevel pointer.
The function in the library takes a argument.
The MATLAB function signatures are:
Pass Multilevel Pointer
This example shows how to pass a multilevel pointer to a C function.
Load the library containing and .
Create a pointer.
Call to allocate memory for the structure.
Free the memory created by the function.
Return Array of Strings
Suppose that you have a library, , with a function, , that reads an array of strings. The function signature is:
The following pseudo-code shows how to manipulate the return value, an array of pointers to strings.
MATLAB creates a object of type . This object points to the first string. To view other strings, increment the pointer. For example, to display the first three strings, type:
Pass Function to Another Function
You can use function handles as input arguments to other functions, which are called function functions . These functions evaluate mathematical expressions over a range of values. Typical function functions include , , , and .
For example, to find the integral of the natural log from 0 through 5, pass a handle to the function to .
Similarly, to find the integral of the function and the function, pass handles to those functions to .
Also, you can pass a handle to an anonymous function to function functions. An anonymous function is a one-line expression-based MATLAB® function that does not require a program file. For example, evaluate the integral of on the range :
Functions that take a function as an input (called function functions) expect that the function associated with the function handle has a certain number of input variables. For example, if you call or , the function associated with the function handle must have exactly one input variable. If you call , the function associated with the function handle must have three input variables. For information on calling function functions with more variables, see Parameterizing Functions.