Rename dynamically columns name of a vector inside of a for loop
1 view (last 30 days)
Show older comments
Sara Alonso Vicario
on 12 Apr 2021
Commented: Mathieu NOE
on 13 Apr 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 Comment
Stephen23
on 13 Apr 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.
Accepted Answer
Mathieu NOE
on 13 Apr 2021
hello Sara
if you stick to the eval method , this is how you should code it :
eval(['d_' num2str(kk) ' = d;'])
2 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!