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 Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by