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

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 Commento

"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.

Accedi per commentare.

 Risposta accettata

hello Sara
if you stick to the eval method , this is how you should code it :
eval(['d_' num2str(kk) ' = d;'])

2 Commenti

Hello Mathieu!
It works, you have helped also in other of my questions!:) I really appreciated!
hello Sara
glad it helps !
have a good day

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB Coder in Centro assistenza e File Exchange

Prodotti

Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by