Run loop for multiple text files

5 views (last 30 days)
ashley
ashley on 27 May 2011
[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')

Accepted Answer

Oleg Komarov
Oleg Komarov on 27 May 2011
data = cell(length(files));
for ii = 1:length(files)
data{ii} = load(files(ii).name);
end
data = cat(1,data{:});
  2 Comments
ashley
ashley on 27 May 2011
Thanks! How do I save matrix A for all eight files, then run the cat function to combine each A?
Oleg Komarov
Oleg Komarov on 27 May 2011
You should replace the snippet in your code.

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!