Arrange data based on filenames

조회 수: 4 (최근 30일)
Daniel
Daniel 2020년 11월 3일
답변: Daniel 2020년 11월 6일
I have many thousand files whose data I would like to arrange in an array in a particular way based on their filenames. Each file is named as XpYt_alwayssame.csv, where X and Y are numbers in a known range and the "_alwayssame.csv" part is always the same. My goal is to put the data (one number per file) into an array that is the length(X) by length(Y). Here's a bit of code to make this clearer:
files = dir([path '*alwayssame*']);
names = {files.name};
% I think there's a way to use the following, but I can't quite figure out how to sort it in two directions, once for X and once for Y.
[~,id] = sort(str2double(regexp(names,'\d*(?=p)','match','once'))); % I think this would order according to X, but not Y
If it helps, X = -5:0.1:9 and Y = 3.5:0.1:10.2, so the files will be named using those numbers where X and Y are. So I'd like the (1,1) entry to correspond to X = -5 and Y = 3.5, then (1,2) would be X = -5 and Y = 3.6, (2,1) would be X = -4.9 and Y = 3.5, etc. Thanks!
  댓글 수: 2
Image Analyst
Image Analyst 2020년 11월 3일
Do X and Y always have one number to the right of the decimal point (even if it's a zero)? Please give us an actual typical filename.
Daniel
Daniel 2020년 11월 4일
편집: Daniel 2020년 11월 4일
No. Sorry, I thought the filenames were clear from what I gave. Here are some examples: -0.1p3.5t_alwayssame.csv, 0.1p3.5t_alwayssame.csv, -1p10.2t_alwayssame.csv, 8.9p4t_alwayssame.csv. So X and Y are literally as given in the question with only the places necessary in the filename (so only one leading zero if there's a decimal and no trailing zeros).

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel
Daniel 2020년 11월 6일
I think I figured out a way to do what I was hoping.
X = -5:0.1:9;
Y = 3.5:0.1:10.2;
path = 'mypath';
files = dir([path '*alwayssame*']); % see above for examples of filenames
names = {files.name};
[~,idx] = sort(str2double(regexp(names,'(?<=p)\d*[.]?\d*','match','once')));
[~,idx2] = sort(str2double(regexp({names{idx}},'.?\d*.?\d*(?=p)','match','once')));
orderednames = names(idx(idx2));
ind = 1;
for i = 1:length(X)
for j = 1:length(Y)
file = importdata([path ordernames{ind}]);
ind = ind+1;
myarray(i,j) = mean(file.data(:,1));
end
end

추가 답변 (1개)

drummer
drummer 2020년 11월 3일
Have you tried to use fileparts?
  댓글 수: 1
Daniel
Daniel 2020년 11월 3일
I don't see how that helps. It looks like it just separates path, filename, and file extension. How are you thinking I would use it?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by