Working in MATLAB CODER with arrays using after calllib and DLL library. Wrong output

5 views (last 30 days)
For instanse, I write some func, like this:
function out = test(in1,in2)
out = in1+in2;
end
Size of inputs in1 and in2 is double 1x2. When I go to MATLAB CODER and generate DLL library. I call these functions:
loadlibrary('test.dll','test.h')
out = calllib('test','test',[1 2],[3 4])
Ouput of this file is only double 1x1. How it can be?

Answers (1)

Harsh
Harsh on 23 Jan 2025
Hi Bob,
While using MATLAB coder to generate dynamic library (DLL), the input parameter dimensions need to be specified correctly. When you run the MATLAB coder app it asks you to define input types. You can enter a script that calls the “test” function here and MATLAB coder will define the input types for you. Check the snapshot below to understand how you can achieve this -
Also, while calling the “test” function you need to pass the output pointer also with pre-allocated space for 1x2 output. Check the code below to understand how to achieve this -
% Define input data
in1 = [1 2];
in2 = [3 4];
out = libpointer('doublePtr', zeros(1, 2)); % Allocate space for 1x2 output
% Call the function
calllib('test', 'test', in1, in2, out);
% Display the result
disp(out.value);

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!