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 件のコメント
Walter Roberson
2019 年 3 月 26 日
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
2019 年 3 月 26 日
Walter Roberson
2019 年 3 月 26 日
"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
2019 年 3 月 27 日
"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
2019 年 3 月 27 日
Stephen23
2019 年 3 月 27 日
" 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
2019 年 3 月 27 日
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
2019 年 7 月 2 日
@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.
回答 (1 件)
Syeda Amberin
2019 年 3 月 26 日
2 件のコメント
Walter Roberson
2019 年 3 月 26 日
s = [Events{1} '_a']
but note this does not create a table variable with that name.
Syeda Amberin
2019 年 3 月 27 日
カテゴリ
ヘルプ センター および File Exchange で Workspace Variables and MAT Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!