Skip to content

Matlab Function Pointer Argumentative Essays

Pass Pointers

Function

The function in the library multiplies the input by .

EXPORTED_FUNCTION double *multDoubleRef(double *x) { *x *= 5; return x; }

The input is a pointer to a , and the function returns a pointer to a . The MATLAB® function signature is:

Return TypeNameArguments

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.

if not(libisloaded('shrlibsample')) addpath(fullfile(matlabroot,'extern','examples','shrlib')) loadlibrary('shrlibsample') end
X = 13.3; Xptr = libpointer('doublePtr',X);
Value: 13.3000 DataType: 'doublePtr'
calllib('shrlibsample','multDoubleRef',Xptr); Xptr.Value

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 .

X = 1:10; xp = libpointer('doublePtr',X); xp.Value
ans = 1 2 3 4 5 6 7 8 9 10

Multilevel Pointers

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.

and Functions

The function in the library takes a argument.

EXPORTED_FUNCTION void allocateStruct(struct c_struct **val) { *val=(struct c_struct*) malloc(sizeof(struct c_struct)); (*val)->p1 = 12.4; (*val)->p2 = 222; (*val)->p3 = 333333; }

The MATLAB function signatures are:

Return TypeNameArguments

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.

if not(libisloaded('shrlibsample')) addpath(fullfile(matlabroot,'extern','examples','shrlib')) loadlibrary('shrlibsample') end
sp = libpointer('c_structPtr');
res = calllib('shrlibsample','allocateStruct',sp)
res = struct with fields: p1: 12.4000 p2: 222 p3: 333333
calllib('shrlibsample','deallocateStruct',sp)

Return Array of Strings

Suppose that you have a library, , with a function, , that reads an array of strings. The function signature is:

Return TypeNameArguments
char** acquireString(void)

The following pseudo-code shows how to manipulate the return value, an array of pointers to strings.

ptr = calllib(myLib,'acquireString')

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:

for index = 0:2 tempPtr = ptr + index; tempPtr.Value end
ans = 'str1' ans = 'str2' ans = 'str3'

See Also

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.

Related Examples

More About

a = 0; b = 5; q1 = integral(@log,a,b)
fun = @(x)x./(exp(x)-1); q4 = integral(fun,0,Inf)