Looping through several files with the same name

4 views (last 30 days)
Hello,
I have the following code to load a number of files with the same name
for i = 1:20
fileName = ['flow' num2str(i)];
load(fileName);
end
All files are the same size (100 * 48). For each file I would like to do the following (i've used {filename} to indicate where the file is being used)
{filename} = rad2deg({filename}); % convert all data to degrees
data = zeros(100, 48); % create an empty array to store data
for g = 1:100
for t = 1:48
data(g, t) = ({filename}(g, t)-nanmean({filename}...(g,:)))/nanstd({filename}(g, :)); % convert all data to Z score
end;
end;
I've tried to use
file = (flow(num2str(i)));
but, for some reason this returns a weird array of numbers
I'm pretty new to MatLab so any help will be greatly appreciated!
Many thanks
  10 Comments
Tracey
Tracey on 29 Oct 2012
1. I'm not quite sure what you mean by a "MATLAB variable" To me, flow1 is exactly the same as an excel spreadsheet - each cell in the 48x100 contains a number. In my workspace S1 is a struct whereas flow1 is a double
2. Apologies for mixing up my 48x100 with my 100x48 I usually transpose the data using ' I didn't in this instance.
Matt J
Matt J on 29 Oct 2012
Edited: Matt J on 29 Oct 2012
MATLAB variables are the named arrays and other quantities that you create in the MATLAB workspace, e.g.
>> a=1; b=[1,2 3]; s='cats and dogs'; %some variables
A .mat file is a file to which you can save collections of variables. It is very different from an Excel spreadsheet, since it may contain different variables of different shapes, sizes and types. The file itself can't be considered a rectangular array of "cells" at all.
The following is a good exercise to get a clearer picture of how .mat files work
>> save myfile.mat a b s
>> whos -file myfile.mat %Look inside the file
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x3 24 double
s 1x13 26 char
As you can see, the above creates a file called myfile.mat which holds not one, but three variables of very different types that were previously created. Furthermore, the variables contained in this file all have their own names, completely independent of the name of the file itself.
When you tell people that you want to load a .mat file, people have to know which of the possibly many variables inside the file you want to load and what their names are. The following will load back in just the variables 'a' and 's' for example
>>load myfile a s

Sign in to comment.

Accepted Answer

Matt J
Matt J on 29 Oct 2012
Edited: Matt J on 29 Oct 2012
So here's a solution to your original question
data=zeros(100,48,20); %data from all 20 files
for i = 1:20
fileName = ['flow' num2str(i)];
S=load(fileName);
thisslice = S.(filename).';
thisslice=thisslice-nanmean(thisslice(:));
thisslice=bsxfun(@rdivide,thisslice,nanstd(thisslice,0,2));
data(:,:,i)=thisslice;
end
  3 Comments
Matt J
Matt J on 30 Oct 2012
I'm not sure what your terminology "structured array" means. Because all of your flow_i matrices are the same size and are pretty small, it was never a good idea to have them in separate files in the first place. That makes them a lot more difficult to simultaneously manipulate. We will undo this first. What you really want to do is stack the matrices in a 3D array and save that stack to one file, which is what the next code segment shows you how to do.
flow=zeros(100,48,20); %flow matrices from all 20 files
for i = 1:20
fileName = ['flow' num2str(i)];
S=load(fileName);
flow(:,:,i) = S.(filename).';
end
save AllMyFlows flow
Now we can begin to do the manipulations you really want. The result of the code below should be a 94x48x20 array 'data' and another array 'flow_clean' of the same dimensions. flow_clean(:,:,i) and data(:,:,i) correspond to the i-th set of flow data.
S=load('AllMyFlows'); %A safer way to load variables
flow=S.flow(1:94,:,:);
rowmeans=nanmean(flow,2);
rowstds=nanstd(flow,0,2);
data=bsxfun(@minus,flow,rowmeans);
data=bsxfun(@rdivide,data,rowstds);
flow_clean=nan(size(data));
flow_clean(abs(data)<=3)=0;
Tracey
Tracey on 30 Oct 2012
Hi Matt,
Thanks so much for your help.
I'm now able to process my data using very few lines of code! Much appreciated!
Tracey

Sign in to comment.

More Answers (1)

George
George on 29 Oct 2012
Tracey,
'flow' is a built-in matlab function for visualizing 3-D data. See >>help flow. Therefore you should not use "file = (flow(num2str(i)));"
Similar to what Matt mentioned, if you load a mat file, using e.g. load('flow15'), then to process it as your code tries, then the array found within each file must have the same name as your filename.
From your posting, I do not really understand what is failing. What, specifically is the problem?
Also, unless you have written a function 'rad2deg', I do not think that Matlab has a built-in rad2deg.
  4 Comments
Tracey
Tracey on 29 Oct 2012
Name Size Bytes Class Attributes
flow1 100x48 38400 double
Tracey
Tracey on 29 Oct 2012
If I type in whos flow1 I get
Name Size Bytes Class Attributes
flow1 100x48 38400 double

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!