Main Content

Use Simulink Strings for C Caller Block

This example shows how to use fixed length and dynamic Simulink® strings for a C Caller block. In this example, the block calls a C function which declares input and output using C strings (char* or char[ ]). The C Caller block receives and outputs Simulink® strings at its input and output ports, respectively.

Open the model that uses simulink strings for block ports using C strings.

mdl = "SimulinkStringCCaller.slx";
open_system(mdl)

C Caller blocks in the Simulink model call C function that declares C string as inputs and outputs.

This model uses the following C string prototypes.

  • char [10] :

void strConcatFcnArg(const char s3[10], const char s4[10], char dst[20])
  • char* :

const char* strConcatFcnReturn(const char* s1, const char* s2);

Fixed Length String

The blocks enclosed in this component calls the strConcatFcnArg function. The C Caller block receives a fixed length Simulink® string at its input ports and outputs the concatenated string. To configure the block for Simulink® strings, in the Port specification table in the Block Parameters dialog box, set Type to stringtype(maximum_length) for each input and output port.

Simulink automatically accounts for the ending null character (\0) for C strings. As a result, the maximum length of the Simulink string you can use is n for a C string of length n+1. For more information, see Simulink Strings and Null Characters.

Here, in this model, a fixed length Simulink string of type stringtype(9) is used for input C string char s3[10].

Fixed length Simulink string for C Caller block.

Dynamic String

The blocks enclosed in this component calls the strConcatFcnReturn function. The C Caller block receives a dynamic Simulink® string at its input ports and outputs a concatenated string. To configure the block for Simulink® strings, in the Port specification table in the Block Parameters dialog box, set Type to string for each input and output port.

Dynamic Simulink string for C Caller block.

Run Simulation and Log Output Data

Run the simulation and log the output data in a Simulink.SimulationOutput object.

out = sim(mdl);

You can access the logged data using dot notation.

ConcatFcnReturnOutput = out.yout{1}.Values.Data;
ConcatFcnArgOutput = out.yout{2}.Values.Data;

See Also

Topics