How can I define the name of a variable that depends on the "i" of a for cycle
4 views (last 30 days)
Show older comments
Hugo Policarpo
on 6 Nov 2020
Commented: Hugo Policarpo
on 6 Nov 2020
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 Comments
Accepted Answer
Ameer Hamza
on 6 Nov 2020
Edited: Ameer Hamza
on 6 Nov 2020
"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 Comments
Ameer Hamza
on 6 Nov 2020
I think this perfectly summarizes your data. This will be an optimal approach, in my opinion.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!