MATLAB: Creating a list of variables

datafor loopvariables

I am writing a program in which I have to load 10 data files. I would like to save these files into variables x1, x2, … , x10. Is there a way I can use a for loop going from 1 to 10 to save the data into these variables with only a few lines of code inside the loop?

Best Answer

This topic comes up quite often. The following link should give you a good guidance.
See 'How can I create variables A1, A2,...,A10 in a loop?' from http://matlab.wikia.com/wiki/FAQ
What is your data file? Is it a .mat file? If it is a .mat file, basically you can do the following:
Files=dir('*.mat');
N=length(Files);
Data=cell(N,1);
for k=1:N
Data{k}=load(Files(k).name);
end