For loop in Simulink

조회 수: 88 (최근 30일)
Mehdi
Mehdi 2015년 1월 17일
댓글: Carsci 2015년 1월 27일
Hi everyone,
I need to convert the following code to a SIMULINK model:
T = [2; 21; 22; 27; 27; 21; 18; 21; 26; 36];
for i = 1:10
C = T(i+1)/T(i);
end
I have used For Iterator Subsystem but I don't know how to apply the i variable to the model. The image of the model is attached herewith. Your help will be very much appreciated.
Mehdi
  댓글 수: 1
Carsci
Carsci 2015년 1월 19일
The question is lacking some boundary information for the case N=10. Specifically the calculation can be done for i = 1 : 9 or for i = 1 : 10. If N = 10 is required what is the value of T(i+1) to be used?

댓글을 달려면 로그인하십시오.

채택된 답변

Carsci
Carsci 2015년 1월 19일
Here is a SIMULINK model that uses an Index Vector block with one-based indexing to get the ith data from T. Another Index Vector block gets the (i+1)th data using the zero-indexing option.
I used an Assignment block is used to create C as a vector of the same size as T.
The For Iterator is specified as i = 1:9 so the last value of C does not get updated but retains the initial value of 36 loaded from T at the start of the simulation.

추가 답변 (1개)

Mehdi
Mehdi 2015년 1월 20일
편집: Mehdi 2015년 1월 20일
Thank you very much for your help. I made the model exactly as you depicted and used index vector and assignment blocks however, I get a 51x10 matrix for the output (C1) in the Workspace. I wonder if I have missed any settings in the assignment block. I have chosen "Index vector (port)" for Index option and "1" for the "number of output dimensions". Am I on the right track?
Thank you for your continued help.
  댓글 수: 1
Carsci
Carsci 2015년 1월 27일
In the workspace you will also have the time dimension. You can try changing the simulation settings so Simulink only calculates one iteration or just decimate the matrix in matlab.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by