Creating variables from iterative variables and output strings?
Show older comments
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 Comments
Stephen23
on 8 May 2016
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
on 8 May 2016
Edited: Rory Donovan
on 8 May 2016
Accepted Answer
More Answers (0)
Categories
Find more on Variables 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!