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
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
2016-5-8
编辑:Rory Donovan
2016-5-8
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!