Problem converting Matlab code to C standalone executable using Matlab Coder

3 visualizaciones (últimos 30 días)
Hello, I am trying to convert a very simple Matlab program (connect two input strings together) to a C standalone executable using Matlab Coder.
The m file I have is:
function r = connect(a,b) %#codegen
assert(isa(a,'char'));
assert(isa(b,'char'));
r = [a,b];
The main.c I have is:
#include <stdio.h>
#include <stdlib.h>
#include "connect.h"
#include "connect_initialize.h"
#include "connect_terminate.h"
int main(int argc, char *argv[])
{
connect_initialize();
printf("Result is %s\n", connect(argv[1],argv[2]));
connect_terminate();
return 0;
}
The error message I got is:
26 C:\C\connect\main.c(16) : warning C4047: 'function' : 'char_T' differs in levels of indirection from 'char *'
27 C:\C\connect\main.c(16) : warning C4024: 'connect' : different types for formal and actual parameter 1
28 C:\C\connect\main.c(16) : warning C4047: 'function' : 'char_T' differs in levels of indirection from 'char *'
29 C:\C\connect\main.c(16) : warning C4024: 'connect' : different types for formal and actual parameter 2
I'd really appreciate it if somebody can give me a hint. Thanks.
  3 comentarios
Hank
Hank el 3 de Sept. de 2012
Hi Walter, thanks for the comment. The connect.h Matlab generated automatically is:
#ifndef __CONNECT_H__
#define __CONNECT_H__
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rtwtypes.h"
#include "connect_types.h"
extern void connect(char_T a, char_T b, char_T r[2]);
#endif
Did you mean the difference between char* and char_T? I also found this in rtwtypes.h:
typedef char char_T;
typedef char_T byte_T;
This might be the problem, but I don't think I understand C well enough to solve it. Could someone please point me the direction? Thanks.
Prachi Singh
Prachi Singh el 25 de Mzo. de 2015
I am also facing a problem while matlab to c conversion. I am working on ECG Analysis using Pan-Tompkins Algorithm I have a function defined as function [ ecg_m,qrs_amp_raw,qrs_i_raw,delay]= pan_tompkin(ecg,fs,gr) but after converting to c using matlab coder I am getting a function void pan_e(const emxArray_real_T *ecg, real_T fs, real_T gr, emxArray_real_T *ecg_m, real_T *delay)
and there is no a mention of qrs_amp_raw,qrs_i_raw which are mainly needed for further processing. Can someone suggest why this problem is occurring?

Iniciar sesión para comentar.

Respuesta aceptada

Kaustubha Govind
Kaustubha Govind el 4 de Sept. de 2012
Editada: Kaustubha Govind el 4 de Sept. de 2012
I think you will find this MATLAB Coder demo useful. To be able to pass in a char buffer, I believe you also need to assert on the length:
function r = connect(a,b) %#codegen
assert(isa(a,'char'));
assert(size(a, 1) <= 1024);
assert(isa(b,'char'));
assert(size(b, 1) <= 1024);
r = [a,b];
Note that the generated prototype also expects the length of the strings to be passed in:
extern void myconnect(const char_T a_data[1024], const int32_T a_size[1], const char_T b_data[1024], const int32_T b_size[1], char_T r_data[2048], int32_T r_size[2]);
  8 comentarios
Walter Roberson
Walter Roberson el 7 de Sept. de 2012
Well that's a pain! No input functions at all, and the only output function is disp(). You are probably going to have to use coder.ceval()
Hank
Hank el 8 de Sept. de 2012
Yeah, luckily disp() is sufficient for output, but it seems I will have to write some code for input conversion.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 3 de Sept. de 2012
That C function signature corresponds to passing a single character to "a" and a single character to "b" and (I think) passing in a pointer to an array of exactly 2 characters to write "r" into.
Note: C expects "strings" to be null terminated, and so a C string with 2 usable characters would need 3 positions, the third for the 0 that terminates it. But it is not an error to construct a character array with only exactly 2 non-zero characters, as long as you do not treat it as a string. But in that code, you do try to treat it as a string, as you want to pass the result directly to printf(). One problem with that -- the function does not return anything and instead writes into an address you pass in in the third parameter.
I do not know why the code is expecting single characters instead of character arrays -- the assert() you are using would normally be suitable for character arrays. You probably need to do something different to alert codegen that you are not working with scalars. I speculate that you will need to initialize r to an array that is the maximum size of the string you will return.
  15 comentarios
Hank
Hank el 7 de Sept. de 2012
Thanks again Walter. I wish I could accept two answers. Your answers helped a lot on the C side.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by