MATLAB Answers

0

How to create an array with the same letter but different number

Asked by Tiago Dias on 17 Oct 2018
Latest activity Commented on by Jan
on 17 Oct 2018
Accepted Answer by Jan
Hi,
How can I create an array or cell, something like Variable = {'X1','X2','X3',...,'X43'}? when i give for example the length = 43.
So when I do my plots, i call put the title as title (variable(i))
Thanks!

  1 Comment

Variable = arrayfun( @(x) ['X', num2str( x )], 1:43, 'UniformOutput', false )
would create them all upfront.

Sign in to comment.

Tags

2 Answers

Answer by Jan
on 17 Oct 2018
 Accepted Answer

Len = 43;
V = sprintfc('X%d', 1:Len);
Or with modern Matlab versions:
V = compose('X%d', 1:Len);
Then:
title(V{i})

  0 Comments

Sign in to comment.


Answer by KSSV
on 17 Oct 2018

for i = 1:10
plot(rand(1,10))
title(sprintf('X%d',i))
drawnow
pause(0.5)
end

  1 Comment

+1: There is no need to store the names in an array, if you can create them dynamically.

Sign in to comment.