How can I define the name of a variable that depends on the "i" of a for cycle
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello MATLAB community,
I would like to improve my code and create different variables with names that depend on the “I” of a for cycle.
What I have is this:
………………………………………………………………………………………………………………………………………………………………
freq_i=1 %initial frequency
freq_d=1000 %final frequency
SEN=1
FRF_RZ_DAM0_KP2_SEN1(:,1)=E02(freq_i+freq_d*(SEN-1):freq_d*SEN); % Frequency [Hz]
SEN=2
FRF_RZ_DAM0_KP2_SEN2(:,1)=E02(freq_i+freq_d*(SEN-1):freq_d*SEN); % Frequency [Hz]
SEN=3
FRF_RZ_DAM0_KP2_SEN3(:,1)=E02(freq_i+freq_d*(SEN-1):freq_d*SEN); % Frequency [Hz]
………………………………………………………………………………………………
So, I get variables that depend on the number of SEN
For SEN=1 I get FRF_RZ_DAM0_KP2_SEN1(:,1)
For SEN=2 I get FRF_RZ_DAM0_KP2_SEN2(:,1)
For SEN=3 I get FRF_RZ_DAM0_KP2_SEN3(:,1)
For the sake of efficiency I would like to create a for loop that could name these variables automatically. For example
for SEN=1:1=30
FRF_RZ_DAM0_KP2_SEN?(:,1)
end for
I believe this is quite easy and have been over it for a while.
Thanks in advance for your help,
Hugo Policarpo
Invited Professor at Instituto Superior Técnico
University of Lisbon
Portugal
댓글 수: 0
채택된 답변
Ameer Hamza
2020년 11월 6일
편집: Ameer Hamza
2020년 11월 6일
"For the sake of efficiency I would like to create a for loop that could name these variables automatically."
This is one of the most inefficient things you can do with your code: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Most efficient way is to save values in numeric or cell arrays.
댓글 수: 5
Ameer Hamza
2020년 11월 6일
I think this perfectly summarizes your data. This will be an optimal approach, in my opinion.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
