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

1 回表示 (過去 30 日間)
Sara Alonso Vicario
Sara Alonso Vicario 2021 年 4 月 12 日
コメント済み: Mathieu NOE 2021 年 4 月 13 日
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 件のコメント
Stephen23
Stephen23 2021 年 4 月 13 日
"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.

サインインしてコメントする。

採用された回答

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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by