Name a variable that includes the cell name from a cell vector

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

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?
I just need to call the table NATO_a. So yes, the table objects should have that name style.
"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:
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.
I apologise for not being experienced with MATLAB. Could you suggest something that experienced ones do?
"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.
Thanks for your reply. I don't see how indexing may help with my situation. Anyways, I am assigning names manually as I don't have very many tables to create. I just wanted to be more efficient.
" 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:
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.
Thanks, Walter. That was very helpful.
@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.

Sign in to comment.

Answers (1)

I figured a round about.
s1='_a';
s=strcat(Events(1),s1);
Producing:
'Nato_a'
Thanks,

2 Comments

s = [Events{1} '_a']
but note this does not create a table variable with that name.
Yes, you're right, it doesn't. Thanks.

Sign in to comment.

Asked:

on 26 Mar 2019

Edited:

on 2 Jul 2019

Community Treasure Hunt

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

Start Hunting!