Importing same file name from multiple folders and find out maximum and minimum of each file?

1 visualización (últimos 30 días)
I have a main folder which contains 50 sub folders. In each sub folder there are 100 of files but i only need to import one '*.out' file from each sub folder which has same file name in 50 sub folders.I want to find out maximum and minimum of each '*.out' file of each sub folders. I am having problem scripting in matlab. Any help will be highly appreciated. Thank you

Respuestas (1)

dpb
dpb el 13 de Mayo de 2016
Editada: dpb el 18 de Mayo de 2016
Here's a location where the OS has better tools than have been carried over into Matlab. I'd do something like
[~,d]=system('dir /s /b yourSpecificWanteFileName.out'); % matching files
for i=1:size(d,1)
...process each file here...
The above presumes the top level directory is your current working directory, if not prepend that directory name to the filename.
Did have meetings and other commitments this afternoon so was somewhat rushed...there are a couple of details this way should mention, the first being (which I did forget initially) that system returns the output as a single string which only looks like the multiple lines because the return text string contains embedded newline \n so do have to do a little fixup before beginning the loop iteration. (Aside and grinch to TMW--why, oh why don't you allow/accommodate command switches in the builtin dir command????)
So, let's begin anew...
[~,d]= cellstr(tokens( ...
system('dir /s /b yourSpecificWanteFileName.out'))); % split dir output by line
N=length(d); % number files found
a=importdata(char(d(1)); % read first file
nCol=size(a,2)-1; % number columns in file less one first column
mnmx=zeros(N,2*nCol); % preallocate for min/max by file
idx=[1:2:2*nCol 2:2:2*nCol]; % place each file min/max adjacent columns
mnmx(1,idx)=[min(a(:,2:end)) max(a(:,2:end))];
for i=2:N % complete the rest having determined size...
a=importdata(char(d(i)); % read the file
mnmx(i,idx)=[min(a(:,2:end)) max(a(:,2:end))];
end
NB: nCol is found by reading the first file before commencing the loop over the remaining files.
You also must decide what you want to do with the results, etc., ...
This presumes that the undescribed .out files are regular array of ASCII delimited so that importdata will succeed; if not will have to fix up the reading to match what the file content is (which again, we don't know).
Also, the tokens function is one of my helper functions as follows--
function tok = tokens(s,d)
% Simple string parser returns tokens in input string s
%
% T=TOKENS(S) returns the tokens in the string S delimited
% by "white space". Any leading white space characters are ignored.
%
% TOKENS(S,D) returns tokens delimited by one of the
% characters in D. Any leading delimiter characters are ignored.
% Get initial token and set up for rest
if nargin==1
[tok,r] = strtok(s);
while ~isempty(r)
[t,r] = strtok(r);
tok = strvcat(tok,t);
end
else
[tok,r] = strtok(s,d);
while ~isempty(r)
[t,r] = strtok(r,d);
tok = strvcat(tok,t);
end
end
Put it in a file tokens.m and make sure it's in a location on the MATLABPATH so will be found.

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by