Create new table per iteration
Show older comments
Hello, as the title says, can I create a new table per for-loop iteration to store the output of that iteration? Also, if the name of that table should be from a predefined array? Can I name this new table by taking it from that array? For example, table_array=[name_1;name_2,... etc], so table 1 will be named "name_1" and table 2 will be named "name_2", etc.
3 Comments
Jon
on 8 May 2023
Not sure the details of your application, but in general I would say it is better to just keep all of the data in one table. You can add a column that lists the iteration number.
Otherwise, if you really think you need to keep multiple tables, you could have one cell array hold all of the tables. It is generally better not to have the indices of your collections as part of the variable name, e.g. name_1, name_2, etc. Instead work with MATLAB's various ways of holding collections of elements, e.g. arrays, cell arrays, arrays of structures, etc. That way when you want to loop through them, or access a particular element, you can naturally use the indices of the collection, rather than trying to somehow generate variable names with indices built in and some kind of text manipulation.
"so table 1 will be named "name_1" and table 2 will be named "name_2""
Rather than forcing meta-data (e.g. psuedo-indices) into the variable names, your code would be simpler, faster, more robust, easier to debug,and much more efficient if you used actual numeric indices. This is also what the creators of MATLAB recommend: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
Accepted Answer
More Answers (0)
Categories
Find more on Tables 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!