Clear Filters
Clear Filters

Creating a string with sprintf() and assign a variable to it

4 views (last 30 days)
Hello
I have a basic question which I can not solve. I am creating multiple variables with sprintf and I would be interested to assign an array to each of them.
I tried to do it in some ways but did not work. Is there any way to do it ?
Thank you in advance

Accepted Answer

Honglei Chen
Honglei Chen on 13 Aug 2012
Edited: Honglei Chen on 13 Aug 2012
Don't know what you are trying to do but here is an example.
eval(sprintf('A%d = %s',1,'magic(3)'))
As a general rule though, you should avoid using variables such as A1, A2, (you may not have this issue, I just put it as a precaution), see
  2 Comments
Dimitris M
Dimitris M on 13 Aug 2012
Thank you very much
I was trying the same thing but in wrong way !!!
I was doing something like
eval(sprintf('Variable_%d',5))=100;
Which did not seem to work !!!!
Thank you anyway !!!
Walter Roberson
Walter Roberson on 13 Aug 2012
Using eval() very often leads to problems in coding and debugging. It is much better to use one of the alternatives such as cell arrays.

Sign in to comment.

More Answers (0)

Categories

Find more on Manual Performance Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!