Using the text in a char matrix as the name of a matrix
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Imagine I have a char matrix as follows:
val =
AAAAA
BBBBB
CCCCC
How can I use these texts as the names of some matrices?
For example I want my first matrix to be:
AAAAA = [3 4 5];
1 Commento
Stephen23
il 9 Mar 2018
Avoid doing this. Dynamically accessing variable names is how beginners force themselves into writing slow, complex, buggy code that is hard to debug. For more information see:
Note that indexing is neat, simple, very easy to debug, and very efficient. You should consider using indexing.
Risposte (3)
Vishwanath Bailore Acharya
il 9 Mar 2018
You can use 'genvarname' for your purpose.
For example, if you have a character array 'val' as,
val = 'ABC';
then you can use 'genvarname' to construct a variable name from the strings as,
a = genvarname(val(1));
then you can use variable 'a' as a variable to any data by using 'eval' function as,
eval([a ' = [1,2,3]']);
Hope this helps.
1 Commento
Walter Roberson
il 9 Mar 2018
Using eval() is not recommended. Using dynamic variable names is not recommended.
Ehsan Khorsandnejad
il 9 Mar 2018
Modificato: Ehsan Khorsandnejad
il 9 Mar 2018
1 Commento
Walter Roberson
il 9 Mar 2018
Creating a variable name from data at run time is making a dynamic variable name. You compute the variable name, such as
varname = str(1:6);
and then you want to use the content of varname as the name of the variable to write to. "Dynamic" in "dynamic variable name" is the opposite of "static" as in "static variable name", with static variable names being the ones that are hard-coded and unchangeable, and dynamic variable names changing depending on the circumstances.
Vedere anche
Categorie
Scopri di più su Performance and Memory in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!