Creating variables from iterative variables and output strings?

This is a question on how to create variables when you don't know exactly how many you might need or need to change frequently. For example, if you have an unknown or undecided amount of features and you want to create a variable to store information for each one. Below shows what I want to put in, but I want to automate it so that for example I only have to type in a a few lines of code to generate a hundred variable names and iteratively repeat the same commands (eg. - mean)
input: 6 categories (t1 , ... , t6) ; 4 features ( max , min , µ , std)
['var_name_goes_here'] = mean(avg_fv4_sub(m*(0:s-1)+1,1));
['var_name_goes_here'] = mean(avg_fv4_sub(m*(0:s-1)+2,2));
...
output:
maxa4_mu_t1 = 159
maxd4_mu_t1 = 12
...
maxd1_mu_t6 = 100

3 件のコメント

Stephen23
Stephen23 2016 年 5 月 8 日
Use a structure instead: this will be easier, faster, and more reliable than any hack code your could invent to dynamically access variable names.
You simply need to stick to using one variable (or a small number), and use indexing (for numeric, cell, structures) or names (structures, tables) to access the data. You idea of creating lots of variable names dynamically, although commonly desired by beginners, is going to be the slowest and buggiest way to write your code.
Rory Donovan
Rory Donovan 2016 年 5 月 8 日
編集済み: Rory Donovan 2016 年 5 月 8 日
So something along the lines of,
X1 = struct('f');
X2 = struct('g')
...
for i = 1:n
a = strcat(...);
X1(i).f = eval(a);
end
For each feature. Thanks
Stephen23
Stephen23 2016 年 5 月 9 日
編集済み: Stephen23 2016 年 5 月 9 日
Nope.
You didn't read my answer and you didn't get the point at all: avoiding the awful eval. Read my answer (and all links) to know why you need to avoid doing this (or read any of the hundreds of other threads on this topic).

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

質問済み:

2016 年 5 月 8 日

編集済み:

2019 年 6 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by