Arrange data based on filenames

4 visualizzazioni (ultimi 30 giorni)
Daniel
Daniel il 3 Nov 2020
Risposto: Daniel il 6 Nov 2020
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 Commenti
Image Analyst
Image Analyst il 3 Nov 2020
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 il 4 Nov 2020
Modificato: Daniel il 4 Nov 2020
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).

Accedi per commentare.

Risposta accettata

Daniel
Daniel il 6 Nov 2020
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

Più risposte (1)

drummer
drummer il 3 Nov 2020
Have you tried to use fileparts?
  1 Commento
Daniel
Daniel il 3 Nov 2020
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?

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by