Create new table per iteration
24 views (last 30 days)
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
Edited: Jon
on 8 May 2023
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.
Stephen23
on 10 May 2023
Edited: Stephen23
on 10 May 2023
"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
Anish Gupta
on 9 May 2023
HI Abd,
As per my understanding, you would like to create a new table while dynamically naming it at each iteration inside a for loop and store them in an arraylike structure.
I would advise you against doing this. The reasons for which can be found here: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval There are other efficient alternatives to it, some of which could be found here: https://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html
If you still want to do this, then you can use the eval function to dynamically name the variables and store them in a cell array. Here is a snippet:
load patients
table_list={};
for i=1:4
eval(['table_',int2str(i),'=table(Age,Height,Weight)']);%You can change the part after equal sign as per your need
eval(['table_list{',int2str(i),'}=table_',int2str(i)]);%storing the created tables in a cell array
end
I hope this resolves the issue you are facing.
5 Comments
Steven Lord
on 10 May 2023
If you must do this (which we strongly recommend you don't) be careful to validate your resource names thoroughly to avoid Exploits of a Mom.
temp_table = 42;
resource_names{2} = '1 = 1; for k = 1:10, why, end; x';
commandToExecute = ['table_',resource_names{2},'=temp_table']
eval(commandToExecute);
I used a relatively benign payload of why and only set it to run for 10 iterations. I could have set it to play the song from handel.mat a thousand times or could have created and started one or more timers to play handel.mat a thousand times some random number of minutes from now, after you've forgotten you ran this code. [And yes, you can play that song in one self-contained command suitable for use in an anonymous function that can be specified as a timer object's TimerFcn. It's not efficient, but if I'm doing that to you I don't really care if it's efficient.]
Or if I didn't just want to be a minor annoyance there are much more dangerous things an attacker could do (like deleting this year's student records, as the administration of Little Bobby Tables's school found out.)
Peter Perkins
on 5 Jun 2023
Let me just add to Steve's strong recommendation. This is the wrong thing to do.
Jon's response was the right thing to do.
More Answers (0)
See Also
Categories
Find more on Logical 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!