Creating variables consisting of many elements

13 views (last 30 days)
I want to create a variable consisting of X1 to X10 . Is there an easy way instead of typing X1 to X10?

Sign in to comment.

Answers (3)

Daniel Burke
Daniel Burke on 4 Aug 2017
From your question it looks like you want V1 to be a cell array of strings, ranging from 'X1' to 'X10', this code should do the trick if that is your goal:
V1 = cell(1,10)
for i = 1:10
V1{i} = ['X' num2str(i)]

Jan on 4 Aug 2017
Or the undocumented function sprintfc:
sprintfc('X%d', 1:10)

Philip Borghesani
Philip Borghesani on 4 Aug 2017
If you don't mind strings and have R2017a I like:
Or if you want a cell array or have 2016b:


Community Treasure Hunt

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

Start Hunting!