This can happen if the generated s-function is not compiled before generating the Simulink block. In this case the block default to one inport and one output. As an example, take the following C code:
"gain.h"
#ifndef _GAIN_H_
#define _GAIN_H_
#include "your_types.h"
extern float gainScalar(const float in, const float in2, const float gain);
#endif
"gainScalar.c"
#include "your_types.h"
#include "gain.h"
float gainScalar(const float in, const float in2, const float gain)
{
return (in+in2*gain);
}
If we use the "legacy_code" function to generate a Simulink block from this C code, we must make sure to compile the code first so that we get the correct number of inports and outports. Here is an example of INCORRECT code that produces only one inport and one outport:
def = [];
def = legacy_code('initialize');
def.SFunctionName = 'sldemo_sfun_st_parameterized';
def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single u2, single p1)';
def.HeaderFiles = {'gain.h'};
def.SourceFiles = {'gainScalar.c'};
def.IncPaths = {''};
def.SrcPaths = {''};
def.SampleTime = 'parameterized';
legacy_code('sfcn_cmex_generate', def);
legacy_code('slblock_generate', def);
We can see that executing this code produces the wrong Simulink block. If instead we execute the following code, we get the correct (two-inport, one-outport) block:
def = [];
def = legacy_code('initialize');
def.SFunctionName = 'sldemo_sfun_st_parameterized';
def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single u2, single p1)';
def.HeaderFiles = {'gain.h'};
def.SourceFiles = {'gainScalar.c'};
def.IncPaths = {''};
def.SrcPaths = {''};
def.SampleTime = 'parameterized';
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def);
legacy_code('slblock_generate', def);
Best Answer