An error occurred when using matlab to call dll file which was generated by matlab coder

  • Hi, everyone! I am using matlab coder to convert m file to dll file using a simple test function. Now I've done the previous job, that is, dll file was generated.
  • However, when using matlab functions(libstruct and calllib)to call the dll file ,an error message occurred: Cannot convert data value for field a due to error: A structure is required.
  • The input argument of the function is a structure "in" with filed "a" and "b". And "a" is a variable-size argument(double inf*inf).
  • When I change the argument "a" and "b" into a double 1*1 size, the error message disappeared. So I wonder if matlab doesn't support the variable-size argument in a dll file, and how can I solve the problem? (The structure is necessary in my algorithm!)
  • Thank you in advance!

 Accepted Answer

When generating code for an Inf X Inf array, Coder will produce a C or C++ function which takes a struct argument to account for the fact that the array is variable size:
If you're intending to use the generated code in MATLAB then change the build target to a MEX file:

3 Comments

  • Thanks s lot Ryan!
  • I've generated mex file, and it can export results just like the original m file. However, what I really intended to do is to get dll file, and make sure that the dll file can work as the original m file.
  • So, is that true that when my mex file can work very well, it also means that my dll file can export results I want? And if not, could you please tell me how can I make sure that the dll file can really work well?
That clarifies the situation for me. The typical steps for verifying generated code are shown at:
So that you'll first generate and test a MEX and then move on to other code verification. If you have an Embedded Coder license, then Software-in-the-Loop (SIL) testing may be beneficial:
SIL wraps up the generated code into a MEX interface which can then be called in MATLAB. However, it has a limitation that it does not support function arguments which are dynamically allocated, like your Inf-by-Inf input. If you can use a fixed or upper bounded argument, then SIL could be of value.
Otherwise, some hand code will likely need to be written to interface with the generated code. An example main function is generated in the folder codegen/dll/<name>/examples which shows how to interact with the generated code:
You could use that example to write one of:
  1. A test script in C or C++ to do the verification
  2. A wrapper that can be easily called by loadlibrary in MATLAB. You could make that wrapper take an array and a size argument which will be easy to pass with calllib and then construct the emxArray argument(s) to call the generated code.
  3. A MEX wrapper which has a mexFunction that takes the inputs from MATLAB, converts them to emxArray arguments, calls the generated code, and passes the output back to MATLAB in plhs.
I feel really grateful for your patient and detailed advice Ryan! That really helps me a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!