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
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 Exchange에서 Variables에 대해 자세히 알아보기

질문:

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