Change the name of a table located in my workspace inside a for loop

60 views (last 30 days)
%Data Folder Analysis
file = dir(fullfile('C:\Users\Desktop\data','*mat'));
num_files=sprintf('The following mat files are to be analyzed: %d',length(file));
%Amount of files analyzed
for i=1:1 %length(file)
current_file = sprintf('Data_set_%d.mat',i);
table_name= table (t,x,y,m)
new_name =sprintf('Table',i);
table_name = new_name;
end
I am trying this but it is not working if the new name that I want to assign is variable.
If I try:
a = table_name;
then the table is saved, but I want to change the name of "a" on evry iteration

Accepted Answer

Athul Prakash
Athul Prakash on 21 Nov 2019
Dynamically changing variable names is not advised, I think you should try storing the data in multiple different tables.
  3 Comments
Walter Roberson
Walter Roberson on 1 Jul 2021
The proper process is not to do it at all.
Do you have a particular context in which you believe that dynamically naming variables is your only choice? If so please describe it. Or is yours just a general question of "Okay, but suppose I wanted to do this?"
The tutorial about why not to do this contains many hints about how it could be done: http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Hugues
Hugues on 21 Dec 2023
I just want to import files and name them during the program play instead of after. Most of my files are the same big tables of test data.
IE: play the import program, since it is the same type of csv table it should import the same way just with a different name in function of the date or user or else. Once the name is defined the table is stored in my workspace and i can work with other functions on it.

Sign in to comment.

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!