Filter löschen
Filter löschen

Arrange data based on filenames

8 Ansichten (letzte 30 Tage)
Daniel am 3 Nov. 2020
Beantwortet: Daniel am 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 = {};
% 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 Kommentare
Image Analyst
Image Analyst am 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 am 4 Nov. 2020
Bearbeitet: Daniel am 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).

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Daniel am 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 = {};
[~,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(,1));

Weitere Antworten (1)

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

Melden Sie sich an, um zu kommentieren.


Mehr zu Programming finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by