MATLAB Answers


How can I assign numbers to variables names?

Asked by Salah Eddine on 15 Mar 2011
Hi all,
How can I assign numbers to variables names without making them vector elements, for example how we write the following for loop: for i=1:5 Ai=i^2; end in such a way that the result is: A1=1 A2=4 . . A5=25


Sign in to comment.

2 Answers

Answer by Paulo Silva
on 15 Mar 2011

That's a very bad idea and there are already several similar questions at matlab answers.


It is almost like we need a FAQ or something...
I bet if we had just two hyper-linked lines show up somewhere in the process of posting a question, something like:
Are you wondering how to make variables like A1, A2, A3, A4, etc?
Are you wondering why == cannot find your number in an array?
That we could reduce these types of posts significantly. I don't think these two most common FAQs would make the Question submission process that much more cumbersome.
on 16 Mar 2011
@Matt: Please send this to Helen ( Or better: Let the user confirm, that the question cannot be solved by reading these two links.

Sign in to comment.

Answer by peter
on 15 Mar 2011

arrayfun(@(x) ['A',num2str(x)],[1:10],'uniformoutput',false)

  1 Comment

Which doesn't accomplishes what the OP asked.

Sign in to comment.