For loop index using not-number variables

good morning,
i was wondering if it possible to create a for loop using names instead number.
i have a list of all states in europe (51), for each state i have 12 month data. I know how to create an idx vector (by state) and then grouping information via logical indexing, but repeating this process 51 times seems to be non professional or maybe is wrong.
there is another way to create a for loop to filter/group data? i was thinking to create a vector with all names, but i don't know if can be used as index in the for loop

4 个评论

"i was wondering if it possible to create a for loop using names instead number... i was thinking to create a vector with all names, but i don't know if can be used as index in the for loop"
It is certainly possible with MATLAB to define the loop iterator to be text (e.g. string scalar), but in practice it is usually much easier to loop over indices than over data.
"there is another way to create a for loop to filter/group data?"
Probably, but as you did not give a description of your data in terms of MATLAB arrays (e.g. size, type, arrangement of the data) nor what data processing you are doing then we can only give general advice, e.g.
Hi, the starting table is 6837x6
and after groupsummary, is reduced to 44 x 4
what i want is to get 44 varibles/tables (one for state) with 12 rows grouping one per month. this task can be done using logical idx and then filtering, but i was wondering if there is a way to do it in a abreviated way instead of repeating 44 times the idx code.
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.
"what i want is to get 44 varibles/tables (one for state) with 12 rows grouping one per month"
In addition to what Walter Roberson wrote: keeping data togther usually makes data easier to work with. Although you could split the table into lots of little tables (without magically creating new variables, e.g. in a cell array) you would then miss out on all of the tools and methods that identify and process groups of data within tables:

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by