Looping through several files with the same name
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Matt J
2012 年 10 月 29 日
編集済み: Matt J
2012 年 10 月 29 日
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
採用された回答
Matt J
2012 年 10 月 29 日
編集済み: Matt J
2012 年 10 月 29 日
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 件のコメント
Matt J
2012 年 10 月 30 日
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;
その他の回答 (1 件)
George
2012 年 10 月 29 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT-Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!