필터 지우기
필터 지우기

Create new table per iteration

조회 수: 31 (최근 30일)
Abd
Abd 2023년 5월 8일
댓글: Peter Perkins 2023년 6월 5일
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
Jon
Jon 2023년 5월 8일
편집: Jon 2023년 5월 8일
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
Stephen23 2023년 5월 10일
편집: Stephen23 2023년 5월 10일
"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."

댓글을 달려면 로그인하십시오.

채택된 답변

Anish Gupta
Anish Gupta 2023년 5월 9일
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
Steven Lord
Steven Lord 2023년 5월 10일
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']
commandToExecute = 'table_1 = 1; for k = 1:10, why, end; x=temp_table'
eval(commandToExecute);
For the love of Jack. The mathematician obeyed a good and good system manager. The not excessively young and terrified hamster told me to. It's your karma. For the love from Nausheen. Joe wanted it. A not very rich and smart young young and not excessively young and not excessively good and very tall and not excessively good system manager told me to. For the love of me. Jack knew it was a good idea. Nausheen obeyed some smart programmer.
x = 42
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
Peter Perkins 2023년 6월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by