I have a variable a = 1. I want to generate a variable name of the form:
variableNumber
So in this example, I would want
a1
a2
a3
as variables. How can I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try
genvarname.is the basic syntax for use. MATLAB documentation has detailed examples of using this function with an exclusion list (for ensuring unique variable names). You will have to use
evalor another function (e.g.assignin, mentioned in an earlier answer) to utilise this variable name.To answer the question completely,
Of course, there are more efficient ways of putting together an input list for
genvarname, this is left as an exercise 😉If you’re concerned about performance, note that
evalmay slow down the script/function greatly; personally I would recommend the use of struct or cell datatypes if you need dynamic variable naming.