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 个评论

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.
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
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 Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by