Rename dynamically columns name of a vector inside of a for loop

2 vues (au cours des 30 derniers jours)
Sara Alonso Vicario
Sara Alonso Vicario le 12 Avr 2021
Commenté : Mathieu NOE le 13 Avr 2021
I want to generate in a loop several vector of values, give to them a name, and append each vector of values with its name as a column in a empty vector I initialized in the beginning.
%to save the final values
detail_coefficients = []; %empty vectors initialize at the beginning
approximation_coefficients = [];
for kk=1:decomposition_level(which_l)
d= wrcoef('d',C,L,wavelet_name(which_l),kk); %generate the vector of values
detail_coefficients = [detail_coefficients, d]; %append the vector
a= wrcoef('a',C,L,wavelet_name(which_l),kk); %generate the vector of values
approximation_coefficients = [approximation_coefficients, a]; %append the vector
end
The code is running as I expected (I have not included in the code here what means C,L, which_l), but my question is,How can I give dynamically names to the columns
(I want the first column generated of d to be d1, the second d2,etc.). I have tried with eval using the following code, but without success
eval(['d_' kk ' = d;'])
Any idea how to give (dynamically) names to the columns?
(I have read in posts about to better save the values in cell arrays, but I found this way easy because after I want to export the vectors detail_coefficients and approximation_coefficients to R.)
  1 commentaire
Stephen23
Stephen23 le 13 Avr 2021
"I found this way easy because after I want to export the vectors detail_coefficients and approximation_coefficients to R"
Exporting will be much easier using indexing, rather than your complex and inefficient approach.

Connectez-vous pour commenter.

Réponse acceptée

Mathieu NOE
Mathieu NOE le 13 Avr 2021
hello Sara
if you stick to the eval method , this is how you should code it :
eval(['d_' num2str(kk) ' = d;'])
  2 commentaires
Sara Alonso Vicario
Sara Alonso Vicario le 13 Avr 2021
Hello Mathieu!
It works, you have helped also in other of my questions!:) I really appreciated!
Mathieu NOE
Mathieu NOE le 13 Avr 2021
hello Sara
glad it helps !
have a good day

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by