How can I define the name of a variable that depends on the "i" of a for cycle
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hugo Policarpo
le 6 Nov 2020
Commenté : Hugo Policarpo
le 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 commentaires
Réponse acceptée
Ameer Hamza
le 6 Nov 2020
Modifié(e) : Ameer Hamza
le 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 commentaires
Ameer Hamza
le 6 Nov 2020
I think this perfectly summarizes your data. This will be an optimal approach, in my opinion.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
