Name a variable that includes the cell name from a cell vector
Show older comments
Hi,
I am trying to assign names to the tables I am creating. I have a 1 by 20 cell vector called "Events" with each cell containing events like "NATO", "G20" etc. Now I need my tables to have names extracted from the cells with extentions like "NATO_a", "NATO_b" etc. I have tried Events(1)_a=table(); but it's not working. How do I extract the cell names and add extensions? Please advise.
11 Comments
Walter Roberson
on 26 Mar 2019
Do you need the table objects to have names such as NATO_a, or do you need table variables to have names such as that?
Syeda Amberin
on 26 Mar 2019
Walter Roberson
on 26 Mar 2019
"I am trying to assign names to the tables I am creating"
Then your data and code are bady designed. Dynamically defining or accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy, hard-to-debug code. Read this to know why:
http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do). You should use indexing, just like experienced MATLAB users do.
Syeda Amberin
on 27 Mar 2019
"Could you suggest something that experienced ones do?"
When you read this thread you will find several suggestions:
In most situations indexing is the simpler solution. Most likely you should just use indexing.
"Now I need my tables to have names extracted from the cells with extentions like..."
Are you sure that you really "need" to do this (e.g. you are required to use a badly-written third party tool which has very specific input specifications, over which you have no control) ? Most of the time when beginners write that they "need" to magically name variables it is because they do not understand how to use indexing.
Syeda Amberin
on 27 Mar 2019
Stephen23
on 27 Mar 2019
" I am assigning names manually as I don't have very many tables to create."
Indexing is much more efficient than changing variable names by hand.
"I just wanted to be more efficient."
What you are trying to do is the opposite of efficient. Read this to know why:
Walter Roberson
on 27 Mar 2019
extensions = {'a', 'b'};
for J = 1 : length(Events)
for K = 1 : length(extensions)
tname = [Events{J} '_' extensions{K}];
MyTables.(tname) = table(with whatever data);
end
end
Dynamic field names but not dynamic variable names.
It is even possible to save these as individual variable names in a .mat:
save('AppropriateFileName.mat', '-struct', 'MyTables');
This would create variables NATO_a, G20_b and so on inside the .mat file, without those variables ever having been created in your workspace.
Syeda Amberin
on 2 Jul 2019
@Syeda Amberin: note that putting meta-data into fieldnames will make your code fragile: consider what your code would do if one of the names was 1ABC, or AB@ (i.e. not a valid fieldname). Meta-data is data, and generally it should be stored in a variable, not in a fieldname or variable name.
Your code would be more robust if you just used indexing.
Answers (1)
Syeda Amberin
on 26 Mar 2019
2 Comments
Walter Roberson
on 26 Mar 2019
s = [Events{1} '_a']
but note this does not create a table variable with that name.
Syeda Amberin
on 27 Mar 2019
Categories
Find more on Workspace Variables and MAT Files 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!