Adding multiple variable contents in a single variable

3 ビュー (過去 30 日間)
Rahul Gulia
Rahul Gulia 2022 年 9 月 19 日
コメント済み: Rahul Gulia 2022 年 9 月 22 日
I hope I can explain my problem statement properly.
I have matrix with name from meanSINR1, meanSINR2, meanSINR3, .... meanSINR10. Each matrix is of size 722x2 double values.
I want to put all these matrix values in a single matrix using a for loop.
I tried using sprintf() to move through each matrix name. But I could get the correct result.
tmp1 = zeros(1,2);
tmp2 = [];
for ii = 1:121
tmp1 = sprintf('meanSINR%d',ii);
tmp2 = [tmp2; tmp1];
end
Looking forward to any kind of suggestion. Thank you.
Kindly let me know if anyone can help me in running through multiple file names in python too using a for loop.
  1 件のコメント
Stephen23
Stephen23 2022 年 9 月 19 日
編集済み: Stephen23 2022 年 9 月 19 日
"I have matrix with name from meanSINR1, meanSINR2, meanSINR3, .... meanSINR10"
Numbering variable names like that is a sign that you are doing something wrong.
Forcing meta-data into variable names is a sign that you are doing something wrong.
Accessing those variables will force you into writing slow, complex, inefficient, insecure, obfuscated code that is buggy and hard to debug. Read this to know some of the reasons why:
So far you have not told us the most important information: how did you get those variables into the workspace? The place where those variables are created in the workspace would be the best place to fix that very unfortunate data design.

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

採用された回答

the cyclist
the cyclist 2022 年 9 月 19 日
If your data are already stored in dynamically named variables, then I think your only recourse is to use eval to do this task:
% Make up some data smaller than your actual data
meanSINR1 = rand(7,2);
meanSINR2 = rand(7,2);
meanSINR3 = rand(7,2);
meanSINR = [];
for ii = 1:3
eval(sprintf('meanSINR = [meanSINR; meanSINR%d];',ii))
end
It would be much better if you could go further upstream in your code, and use cell arrays or multi-dimensional arrays to store these data. For example, the above could instead have been
meanSINR = rand(7,2,3);
from the beginning, with all the data stored in a single array.
  6 件のコメント
Rahul Gulia
Rahul Gulia 2022 年 9 月 22 日
I really appreciate to hear back from @the cyclist and @Stephen23 on my issues. Thank you for your time and patience to help resolve my issue.
I will surely learn to do efficient coding in MATLAB, and hope someone else also learns from my mistakes.

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

その他の回答 (0 件)

カテゴリ

Find more on Historical Contests 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!

Translated by