Run loop for multiple text files

[EDIT: Fri May 27 04:38:19 UTC 2011 - Reformat - MKF]
I want to run a loop, then build a matrix for 8 data files. Error message: Undefined function or variable 'data'. keeps popping up. what do I do?
%Data for BrainZ sample_i, for i=1:8,
%[Data] -> [N' i] , 24(rows) by 2(col), for BrainZ sample_i, for i = 1:8
%col(1) = Region of Interest sample #, col(2) = mean density (tab delimited data)
%row = all measurements(1:24)
files = dir('*.txt');
for i=1:length(files)
load(files(i).name);
end
%Re-arrange [Data] to form new matrix [A]
%[A] -> [N" i] = 4(rows) by 6(col), of BrainZ sample_i, for i = 1:8
%row = measurement_h, for h = 1:4, from Region of Interest (ROI)
%col = ROI = [mU mM mL bU bM bL]
%Monoc Upper = mU, Monoc Middle = mM, Monoc Lower = mL, Bi Upper = bU, Bi Middle = bM, Bi Lower = bL
for n=1:1:size(data,1)
if n<=4
mU(n)=data(n,2);
elseif n>=5 && n<=8
mM(n)=data(n,2);
elseif n>=9 && n<=12
mL(n)=data(n,2);
elseif n>=13 && n<=16
bU(n)=data(n,2);
elseif n>=17 && n<=20
bM(n)=data(n,2);
elseif n>=21 && n<=24
bL(n)=data(n,2);
else
end;
end;
mU(mU == 0) = [];
mM(mM == 0) = [];
mL(mL == 0) = [];
bU(bU == 0) = [];
bM(bM == 0) = [];
bL(bL == 0) = [];
A = cat(2, mU', mM', mL', bU', bM', bL')

 采纳的回答

data = cell(length(files));
for ii = 1:length(files)
data{ii} = load(files(ii).name);
end
data = cat(1,data{:});

2 个评论

Thanks! How do I save matrix A for all eight files, then run the cat function to combine each A?
You should replace the snippet in your code.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by