0
0
SimulinkHow-ToBeginner · 2 min read

Simulink How to Convert Model to Structured Text

Use Simulink Coder by opening your model and selecting Code > C Code > Generate Code and then choose Structured Text as the output language to convert your Simulink model to structured text code.
📋

Examples

InputSimple Gain block model
OutputGenerated structured text code representing the gain operation, e.g., output = input * gain;
InputModel with If-Else logic blocks
OutputStructured text code with <code>IF</code> and <code>ELSE</code> statements reflecting the model logic
InputModel with Stateflow chart
OutputStructured text code including state machine logic translated from Stateflow
🧠

How to Think About It

To convert a Simulink model to structured text, you first prepare the model for code generation by ensuring it uses supported blocks. Then, you use Simulink Coder to generate code and select structured text as the output format. This process translates graphical blocks into readable structured text code that represents the model's logic.
📐

Algorithm

1
Open your Simulink model in MATLAB.
2
Ensure the model uses blocks supported for code generation.
3
Go to the Simulink Coder menu and select 'Generate Code'.
4
In the code generation settings, choose 'Structured Text' as the output language.
5
Run the code generation process to produce the structured text code file.
6
Review and use the generated structured text code as needed.
💻

Code

matlab
model = 'simple_gain';
load_system(model);
generateCode(model, 'TargetLang', 'StructuredText');
disp('Structured Text code generated for model: simple_gain');
Output
Structured Text code generated for model: simple_gain
🔍

Dry Run

Let's trace generating structured text code for a simple gain model.

1

Load Model

Load the 'simple_gain' model into Simulink.

2

Set Code Generation Language

Set the target language to 'StructuredText' in code generation settings.

3

Generate Code

Run code generation to produce the structured text file.

StepActionResult
1Load model 'simple_gain'Model loaded successfully
2Set target language to StructuredTextSettings updated
3Generate codeStructured text code file created
💡

Why This Works

Step 1: Model Preparation

The model must use blocks supported by Simulink Coder to ensure successful code generation.

Step 2: Selecting Structured Text

Choosing structured text as the output language tells Simulink Coder to translate the model into readable text code.

Step 3: Code Generation

Simulink Coder processes the model and outputs structured text code that replicates the model's logic.

🔄

Alternative Approaches

Manual Translation
matlab
/* Manually write structured text code based on model logic */
IF input > threshold THEN
  output := input * gain;
ELSE
  output := 0;
END_IF;
This approach is error-prone and time-consuming but useful for small models or custom logic.
Using MATLAB Function Block
matlab
function y = fcn(u)
  y = u * gain;
end
Write MATLAB code inside a function block and generate structured text from that code, useful for custom algorithms.

Complexity: O(n) time, O(n) space

Time Complexity

Code generation time grows linearly with the number of blocks in the model because each block is translated once.

Space Complexity

Generated code size grows with model complexity, requiring space proportional to the number of blocks and connections.

Which Approach is Fastest?

Using Simulink Coder is fastest and most reliable; manual translation is slow and error-prone.

ApproachTimeSpaceBest For
Simulink CoderO(n)O(n)Automated, large models
Manual TranslationO(n^2)O(n)Small or custom logic
MATLAB Function BlockO(n)O(n)Custom algorithms within Simulink
💡
Always verify your model uses code generation compatible blocks before generating structured text.
⚠️
Trying to generate structured text from unsupported blocks causes errors or incomplete code.