Legacy Tool s-function with an array input problem

3 visualizzazioni (ultimi 30 giorni)
I am trying to learn how to use legacy code tool but I am not succeding, maybe because I am a beginner in C, but also because the documentation it is not so complete.
I have this C function add_array, the main is just to test the output:
#include <stdio.h>
void add_array (int *a, int num_elements, int *final);
int main()
{
int Tab[5] = {1000, 220, 37, 16, 98};
int final;
add_array(Tab, 5, &final);
printf("Total summation is %d\n", final);
return 0;
}
void add_array (int *p, int size, int *final)
{
int total = 0;
int k;
for (k = 0; k < size; k++)
{
total += p[k]; /* it is equivalent to total +=*p ;p++; */
}
*final = total;
}
Which output is the sum of the element of the int array: Total summation is 1371
I have followed this example Inherited Signal Dimensions for Legacy Function Arguments - MATLAB & Simulink - MathWorks Italia and I have rewritten add_array function like this(I called this c file vegeta3.c):
#include <stdio.h>
#include <stdint.h>
#include "add_veg.h"
extern void add_array (int32_T *p, int32_T size, int32_T *final)
{
int32_T total = 0;
int32_T k;
for (k = 0; k < size; k++)
{
total += p[k]; /* it is equivalent to total +=*p ;p++; */
}
*final = total;
}
I have also created an header called add_veg.h:
#ifndef ADD_VEG_H_INCLUDED
#define ADD_VEG_H_INCLUDED
#ifdef MATLAB_MEX_FILE
#include "tmwtypes.h"
#else
#include "rtwtypes.h"
#endif
void add_array (int32_T *p, int32_T size, int32_T *final);
#endif // ADD__VEG_INCLUDED
And this is my matlab script to create the s function with the legacy tool:
%% define S-Function
sf = legacy_code('initialize'); %initialize s-function structure
sf.SFunctionName = 'vegekou';
sf.OutputFcnSpec = 'void(int32 u1[5], int32 u2, int32 y1)';
sf.HeaderFiles = {'add_veg.h'};
sf.SourceFiles = {'vegeta3.c'};
%% create new c file suitable for mex compilation
legacy_code('sfcn_cmex_generate', sf);
%% compile the sdunction with MEX
legacy_code('compile',sf);
I receive a lot of errors when trying to compile:
Error using mex
vegekou.c
C:\sfun\c_code\return_array\vegekou.c(168): error C2143: syntax error: missing ')' before ','
C:\sfun\c_code\return_array\vegekou.c(168): error C2040: 'u1': 'int' differs in levels of indirection from 'int32_T *'
C:\sfun\c_code\return_array\vegekou.c(168): error C2086: 'int32_T *u2': redefinition
C:\sfun\c_code\return_array\vegekou.c(163): note: see declaration of 'u2'
C:\sfun\c_code\return_array\vegekou.c(168): error C2059: syntax error: ')'
C:\sfun\c_code\return_array\vegekou.c(168): error C2086: 'int32_T *y1': redefinition
C:\sfun\c_code\return_array\vegekou.c(164): note: see declaration of 'y1'
Error in legacycode.LCT/compile
Error in legacycode.LCT.legacyCodeImpl
Error in legacy_code (line 101)
[varargout{1:nargout}] = legacycode.LCT.legacyCodeImpl(action, varargin{1:end});
Error in legacy_vegeta (line 12)
legacy_code('compile',sf);
What am I doing wrong and how can I solve this?
I do not want to have y1 as output on the right of = in sf.OutputFcnSpec and add_array as a return function. Because I would like in the future to write a similar void function where there is an array output and I have observed that if y1 is a normal output on the right , legacy tool gives error saying that I can only use integers as outputs.
Thanks a lot in advance.

Risposte (1)

Maneet Kaur Bagga
Maneet Kaur Bagga il 17 Nov 2023
Hi Claudio,
As per my understanding, the errors encountered may be because of the following reasons:
  • The "sf.OutputFcnSpec" should match the signature of the "add_array" function in the C code. The correct specification should be "void(int32_T u1[5], int32_T u2, int32_T *y1)".
  • The errors "error C2086: 'int32_T *u2': redefinition and error C2086: 'int32_T *y1': redefinition" are caused by redefining the input arguments u2 and y1 in the C code. The modified C code is as follows:
extern void add_array(int32_T *p, int32_T size, int32_T *final)
{
int32_T total = 0;
int32_T k;
for (k = 0; k < size; k++)
{
total += p[k];
}
*final = total;
}
  • In the "add_veg.h" header file, two header files are included either "tmwtypes.h" or "rtwtypes.h" based on whether it's a MATLAB MEX file or not. These header files are not necessary for the current code and can be removed.
Please refer to the following MATLAB Documentation for better understanding of the S-Function:
Hope this helps!

Categorie

Scopri di più su Simulink Coder in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by