How to select variable based on numbers in variable name?

I have variables named as X1, X2, X3,...,X72. Is there a way to place them in a vector based on the number in the variable name? For example, suppose I want to put the multiples of 5 in a vector: vec=[X5 X10 X15...X70]. However, I want the value assigned to the variable and not the variable name to be displayed.

1 件のコメント

Robert Cumming
Robert Cumming 2011 年 6 月 2 日
can you show some of your code to get a better idea of what you are doing?

サインインしてコメントする。

回答 (2 件)

Jason Ross
Jason Ross 2011 年 6 月 2 日

0 投票

Do you need to create all these variables with the names, or you could you make an array called X and then use the index?
X = [10 20 30 40 50]
vec = [X(1) X(3)]
Otherwise, it sounds like you are going to be reinventing a lot of stuff that is already implemented.

4 件のコメント

Laura
Laura 2011 年 6 月 2 日
I am uploading a series of 72 .dat files into Matlab. Each of these files has a single number, which Matlab assigns the variable names X1,X2,...
So I think I am stuck with using all the variables.
Jason Ross
Jason Ross 2011 年 6 月 2 日
What method are you using to actually get the data in?
Laura
Laura 2011 年 6 月 2 日
files = dir('*.dat');
Jason Ross
Jason Ross 2011 年 6 月 2 日
So that will give you a list of the .dat files in the directory. I'm assuming there is some code that actually opens the filename and then reads the content of the file (which I understand to be a single number).
e.g.
example1.dat contains "12345"
example2.dat contains "678910"
and so on for 72 times.

サインインしてコメントする。

カテゴリ

質問済み:

2011 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by