Can variable size matrices be used as inputs with coder.ceval?

4 vues (au cours des 30 derniers jours)
Carlo Savorgnan
Carlo Savorgnan le 12 Avr 2016
Réponse apportée : upol le 27 Déc 2018
I would like to use from MATLAB a C function which takes as an argument a matrix of variable size and returns the sum of its elements. The resulting code needs to be compatible for use with Matlab coder. The function is defined in the header "my_sum.h":
#ifndef __MY_SUM_H__
#define __MY_SUM_H__
#ifdef MATLAB_MEX_FILE
#include <tmwtypes.h>
#else
#include "rtwtypes.h"
#endif
double my_sum(double * data, int rows, int cols);
#endif
The function is defined inside the file "my_sum.c":
#include "my_sum.h"
double my_sum(double * data, int rows, int cols){
double tmp=0;
for (int ind=0; ind<rows*cols; ++ind){
tmp += data[ind];
}
return tmp;
}
I defined the following MATLAB interface (file "call_my_sum.m"):
function out = call_my_sum(mat)
%#codegen
assert(isa(mat, 'double'));
if coder.target('MATLAB')
out = call_my_sum_mex(mat);
else
[rows, cols] = size(mat);
out = double(0);
out = coder.ceval('my_sum', coder.ref(mat), rows, cols);
end
Finally I created the mex interface "call_my_sum_mex" using the command:
>> codegen -config:mex call_my_sum my_sum.h my_sum.c
The compilation completed but I can use the call_my_sum only with a scalar input. If I run the following code
>> mat = ones(3,3);
>> call_my_sum(mat)
I get the error: MATLAB expression 'mat' is not of the correct size: expected [1x1] found [3x3].
How should I change my code to make it work with an input of any size?
Thanks
Carlo

Réponse acceptée

Carlo Savorgnan
Carlo Savorgnan le 12 Avr 2016
I found how to solve my problem. I share the solution for other people running into the same error.
The error can be eliminated by using coder.varsize inside the file "call_my_sum.m":
function out = call_my_sum(mat)
%#codegen
coder.varsize('mat', [inf, inf], [1 1]);
assert(isa(mat, 'double'));
if coder.target('MATLAB')
out = call_my_sum_mex(mat);
else
[rows, cols] = size(mat);
out = double(0);
out = coder.ceval('my_sum', coder.ref(mat), rows, cols);
end

Plus de réponses (2)

Walter Roberson
Walter Roberson le 12 Avr 2016
You initialized out to a scalar before the call. It needs to initialized to zeros(rows, cols)
  1 commentaire
Carlo Savorgnan
Carlo Savorgnan le 12 Avr 2016
Dear Walter, I didn't get what I should do exactly. Could you be more specific? Thanks Carlo

Connectez-vous pour commenter.


upol
upol le 27 Déc 2018
I am trying to convert this simple code into excutable using matlab coder.
function y = hello_world
%#codegen
y = 'Hello World!';
converting to source code C works but when i change the build type to Executable
It gives me this error: Build error: C compiler produced errors. See the Build Log for further details.
C:/PROGRA~3/MATLAB/SUPPOR~1/R2018b/3P778C~1.INS/MINGW_~1.INS/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/lib/../lib\libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
gmake: *** [C:/dummy/hello_world.exe] Error 1
The make command returned an error of 2
'An_error_occurred_during_the_call_to_make' is not recognized as an internal or external command,
operable program or batch file.
Error(s) encountered while building "hello_world":
### Failed to generate all binary outputs.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by