return array from c++ dll to matlab

user130227

I am trying to build a DLL in C++ and call it from MATLAB by using loadlibrary and calllib instructions. It works for one value and it return a value normally, but now I am trying to return a whole array from C++ DLL to MATLAB as an output of a function.

As you know C++ usually return the arrays as pointers but this don't work with MATLAB ... I searched for that at the internet and they are using some MEX function but it is not clear ...

Can you explain how to return an array from C++ DLL to MATLAB calllib and how should we return it from C++ code ?

Amro

Consider a DLL that exposes the following C function:

void getData(double *x, const int len)
{
    for(int i=0; i<len; i++) {
        x[i] = i;
    }
}

It takes an array already allocated an its length, and fills it with incremental values.

In MATLAB, first we load the library:

>> loadlibrary('mydll.dll', 'mydll.h')
>> libfunctions mydll -full

Functions in library mydll:

doublePtr getData(doublePtr, int32)

To call the exposed function, we use libpointer:

>> p = libpointer('doublePtr', zeros(1,10))  % initialize array of 10 elements
p =
libpointer
>> get(p)
       Value: [0 0 0 0 0 0 0 0 0 0]
    DataType: 'doublePtr'
>> calllib('mydll', 'getData', p, 10)        % call C function
>> get(p)
       Value: [0 1 2 3 4 5 6 7 8 9]
    DataType: 'doublePtr'

we could also simply pass regular vectors and MATLAB will take care of marshalling:

>> x = calllib('mydll', 'getData', zeros(1,10), 10)
x =
     0     1     2     3     4     5     6     7     8     9

note that in this case, the modified array will be returned as an output (since builtin types will not be modified in-place).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return an array from a c# dll to php

From Dev

Return a dynamically allocated array from C++ dll to VBA

From Dev

Return String array from C++ DLL to VBA(Excel)

From Dev

How to call a C DLL from a VBA application and return an array?

From Dev

how to return byte array , uint value from c# dll, dll called by c++/cli

From Dev

How to get return array from function with global variable from C++ dll in C#?

From Dev

How to get return array from function with global variable from C++ dll in C#?

From Dev

return struct from .net dll (C#)

From Dev

return struct from .net dll (C#)

From Dev

Return an array of frames from a video in MATLAB

From Dev

Return a fake value from external DLL using C#

From Dev

C++ How to get the right return from dll function call

From Dev

MEX: How to return a matrix from C++/ C to MATLAB

From Dev

Passing a char array from c# to c++ dll

From Dev

Returning int array from c++ .dll to c#

From Dev

C++ - Return multidimensional array from function

From Dev

Return a pointer to array from a function in C++?

From Dev

How to "return" an array from a function to main in c

From Dev

C++ - Return multidimensional array from function

From Dev

Return a pointer to array from a function in C++?

From Dev

Return struct array from function c++

From Dev

How to return array from database in c#

From Dev

C: how to return array of numbers from a function

From Dev

calling function with array parameters in c++ dll from Delphi

From Dev

Pass double array by reference from C to Delphi DLL

From Dev

Returning the Sum of 2 Dimensional Array elements from DLL C#

From Dev

Calling a C# DLL from Delphi, with array of byte parameter

From Dev

Getting char array from c dll in python as string

From Dev

Return string from c++ dll export function called from c#

Related Related

  1. 1

    How to return an array from a c# dll to php

  2. 2

    Return a dynamically allocated array from C++ dll to VBA

  3. 3

    Return String array from C++ DLL to VBA(Excel)

  4. 4

    How to call a C DLL from a VBA application and return an array?

  5. 5

    how to return byte array , uint value from c# dll, dll called by c++/cli

  6. 6

    How to get return array from function with global variable from C++ dll in C#?

  7. 7

    How to get return array from function with global variable from C++ dll in C#?

  8. 8

    return struct from .net dll (C#)

  9. 9

    return struct from .net dll (C#)

  10. 10

    Return an array of frames from a video in MATLAB

  11. 11

    Return a fake value from external DLL using C#

  12. 12

    C++ How to get the right return from dll function call

  13. 13

    MEX: How to return a matrix from C++/ C to MATLAB

  14. 14

    Passing a char array from c# to c++ dll

  15. 15

    Returning int array from c++ .dll to c#

  16. 16

    C++ - Return multidimensional array from function

  17. 17

    Return a pointer to array from a function in C++?

  18. 18

    How to "return" an array from a function to main in c

  19. 19

    C++ - Return multidimensional array from function

  20. 20

    Return a pointer to array from a function in C++?

  21. 21

    Return struct array from function c++

  22. 22

    How to return array from database in c#

  23. 23

    C: how to return array of numbers from a function

  24. 24

    calling function with array parameters in c++ dll from Delphi

  25. 25

    Pass double array by reference from C to Delphi DLL

  26. 26

    Returning the Sum of 2 Dimensional Array elements from DLL C#

  27. 27

    Calling a C# DLL from Delphi, with array of byte parameter

  28. 28

    Getting char array from c dll in python as string

  29. 29

    Return string from c++ dll export function called from c#

HotTag

Archive