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')

 採用された回答

Oleg Komarov
Oleg Komarov 2011 年 5 月 27 日

0 投票

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

2 件のコメント

ashley
ashley 2011 年 5 月 27 日
Thanks! How do I save matrix A for all eight files, then run the cat function to combine each A?
Oleg Komarov
Oleg Komarov 2011 年 5 月 27 日
You should replace the snippet in your code.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by