dynamic matrix with changing variable name in for loop

3 ビュー (過去 30 日間)
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer 2022 年 9 月 20 日
編集済み: Stephen23 2022 年 9 月 21 日
Hi,
I want to rename each matrix in a loop. There are two variable 'mat" and "ser". I tried the code below. Did not work. Any recommendations? Thank you
E_list=[ 1.2E-4, 1.2E-3, 1.2E-2, 1.2E-1, 1, 10, 100]
mat='Glass'
ser=24
for i=1:size(E_list)-1
..... Some calculations to get objective and popt below
......
([mat,'_parameters_',num2str(ser)])(i,1)=E_list(i)
([mat,'_parameters_',num2str(ser)])(i,2))=objective(popt)
([mat,'_parameters_',num2str(ser)])(i,3:6))=popt % Parameters
end
  1 件のコメント
Stephen23
Stephen23 2022 年 9 月 21 日
編集済み: Stephen23 2022 年 9 月 21 日
Forcing numbers (e.g. "ser") into variable names is a sign that you are doing something wrong.
Forcing meta-data (e.g. "Glass") into variable names is a sign that you are doing something wrong.
Meta-data is data, and data belongs in variables, not in variable names.
MATLAB is designed to work simply and efficiently using indexing. You should use indexing.

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

回答 (1 件)

James Tursa
James Tursa 2022 年 9 月 20 日
Do not dynamically name variables. This is bad programming practice and will leave you with hard-to-use variables downstream in your code. Instead, use cell arrays or struct arrays etc. See this link for more discussion:

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by