Arrange data based on filenames

4 次查看(过去 30 天)
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