Simulink Tutorial Series – 4 [For Loop]
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 –
- How to start Matlab
- How to open the Simulink from Matlab
- How to open a Blank Simulink Model
- How to open Simulink Library Browser
- How to find a block in Simulink Library Browser
- How to implement if-else logic in Simulink Model
- 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:
- By using the For Iterator Library Block
- 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.
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”:
- Iteration Limit Source: I set it as external. So, I can easily control the iteration number using an input port.
- 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.
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.
For Loop in Simulink Model – Output
Now, I am going to execute the model and see if I am getting the right 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.
Now, I have dragged and dropped al other required Simulink library blocks and connected them:
I have to update the matlab function by double clicking the matlab function block.
For Loop in Simulink Model – Output
I can now go back to the model and execute it by hitting the green play button:
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!
This post was published by Admin.
Email: admin@TheCloudStrap.Com