Simulink Tutorial Series – 4 [For Loop]

Simulink Tutorial Series - 4

If you are looking for a quick solution about how to implement the For Loop in Simulink model , You have landed on the right page.

I will explain two different methods to implement a For loop in Simulink model.

In this Simulink Tutorial Series, I am going to discuss real-life examples using Simulink model and build the Simulink model step-by-step.

Matlab/Simulink is the leading software for model based development in aerospace, automotive industry etc.

There are several other online resources, where you can get theoretical knowledge about Matlab/Simulink. In fact, Matlab has very good documentation for each of their products. But, there are not many resources to explain the theory along with good working examples. This is going to be a series of articles. This is the 4th article in this series.

In every article in this Simulink Tutorial Series, I will add real-life working examples and show how to build models for a particular problem.

What Will You Learn?

In this article, I am going to explain to you how you can implement “for-loop” in the Simulink model.

If you are familiar with other programming languages such as C, C++, Java, or any other programming languages, you already know that every programming language has a “for loop” construct.

Similarly, In Simulink, we can build a model including “for loop” logic.

So, if you go through this article, you will be able to build your own Simulink model using “for loop” logic.

Assumption

I am assuming, you already gone through my previous article –

Simulink Tutorial Series – 1
Simulink Tutorial Series – 2
Simulink Tutorial Series – 3

I have explained basic understanding of Simulink and “if-else” logic implementation in Simulink.

What You already know

From my previous posts, you already know –

  1. How to start Matlab
  2. How to open the Simulink from Matlab
  3. How to open a Blank Simulink Model
  4. How to open Simulink Library Browser
  5. How to find a block in Simulink Library Browser
  6. How to implement if-else logic in Simulink Model
  7. How to implement switch-case construct in Simulink Model

I have discussed all the above points in my previous post.

What is For Loop in Simulink?

If you already know for loop in Simulink, you can skip this section and move to the next bullet point. Most of you may have previous coding experience and understand the for loop construct.

For loop allows the programmer to execute a statement or a set of statements multiple times. Here is an example of for loop in c programming language:

	int strap_count;
	for(strap_count = 0; strap_count <= 10; strap_count++)
	{
		printf("%d\n",strap_count);
	}

This is a for loop example implemented in C programming language. The for loop initializes the strap_count to 0 and keep executing the body of the for loop until the strap_count reaches to 11. In each iteration, the strap_count is incremented by 1.

The for loop in Simulink works in the same way.

Today, I am going to explain how to implement the for loop in Simulink.

How to Implement For Loop in Simulink?

There are two ways to implement the for loop in Simulink model:

  1. By using the For Iterator Library Block
  2. By using the Matlab function

In this post, I will explain you both of these methods.

Method-1 : Implement For Loop in Simulink

There is a For Iterator library block in Simulink. In method-1, I will use this library block and build the model.

In the previous posts, I have clearly explained how to add the Simulink library blocks onto the Simulink blank model/canvas. Therefore, I am going to find all the required Simulink library blocks and dropping them onto Simulink blank model.

Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model
For loop in Simulink

Now, I will double click on the For loop subsystem and configure the For Iterator block. I will ensure the following setting in the “Block Parameters: For Iterator”:

  1. Iteration Limit Source: I set it as external. So, I can easily control the iteration number using an input port.
  2. Index Mode: I have selected here “One-based”. However, if you want to generate the code from the model, then you should select “Zero-based” option here.
Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model
For loop in Simulink – For Iterator Setting

Now, here is how the For Iterator sub-system looks like. I have added add block and delay block. So, clearly, I want to add the numbers starting from 1 to 10.

Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model
For loop in Simulink – For Iterator

For Loop in Simulink Model – Output

Now, I am going to execute the model and see if I am getting the right output:

Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model
For loop in Simulink Model – Output

So, clearly, the expected output is displayed here.

Method-2 : Implement For Loop in Simulink

Now, let’s look at the 2nd method by using a Matlab function.

You can find the Matlab function library block under the “User-Defined functions” section in the Simulink library browser.

Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model
Matlab function

Now, I have dragged and dropped al other required Simulink library blocks and connected them:

Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model; Matlab Function
For Loop In Simulink – Method – 2

I have to update the matlab function by double clicking the matlab function block.

Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model; Matlab Function
Matlab Function

For Loop in Simulink Model – Output

I can now go back to the model and execute it by hitting the green play button:

Simulink Tutorial Series - 4; For loop in Simulink; For Loop in Simulink Model; Matlab Function
For loop in Simulink Model – Output

Download Sample Model

You can download the Sample Model by Clicking HERE.

Summary

Matlab/Simulink is a well known and very popular tool used for Model Based Software Development in the aerospace and automotive industry.

Today, in this article (For Loop in Simulink Model), I have explained step-by-step, how to implement for loop in the Simulink model using Simulink library blocks from scratch.

If you have any questions, please feel free to comment in the comment box below. 👇👇👇

I will keep sharing useful real-life Simulink model example here – Simulink Tutorial Series.

Stay Tuned!

Simulink Tutorial Series – 4
Scroll to top
error: Content is protected !!