-2

I am designing a GUI in Matlab,

I have a folder called sth. It contains many files having same structure like,

filename_1_something.mat
filename_2_something.mat

In order to loop over filenames by selecting via index, I need to find a resulting string like this;

filename_%d_something.mat

So I don't need to read all the files in the dir. Two of the filenames are enough to compare strings and find the different char array item and change by %d.

Or anything different than this also appreciated.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
freezer
  • 531
  • 1
  • 11
  • 28
  • what's the question? – rock321987 May 23 '16 at 08:42
  • Haha, I am asking that, how can I find the desired filename pattern out of existing at most 100 filenames inside of a folder. – freezer May 23 '16 at 08:44
  • use this regex :- `filename_\d+_something.mat` for comparing file name – rock321987 May 23 '16 at 08:46
  • Yeah, the problem is, user will select the folder and I don't have any info about the filenames inside of the folder. Filename strcuture can be anything but index number somewhere in it. – freezer May 23 '16 at 08:46
  • I saw your regex. But I can't use regex I guess. I don't have the pattern. I need to find the pattern – freezer May 23 '16 at 08:49
  • aha, so the structure can be anything? its not known beforehand? – rock321987 May 23 '16 at 08:50
  • Yeap. That is the problem. Actually, dir function in Matlab gets the filenames as array of struct. But the order of files are not correct. it follows like fn_100_.mat, fn_10_.mat, fn_1_.mat, fn_2._mat etc. – freezer May 23 '16 at 08:50
  • i don't follow you..is the structure of names like `filename_[digits]_something.mat` – rock321987 May 23 '16 at 08:56
  • I am just giving examples :). strings before and after of digits can be anything :). Your patern is ok for me. – freezer May 23 '16 at 08:58
  • there are two underscores before and after digits? – rock321987 May 23 '16 at 09:00
  • 1
    in that case you can use this regex :- `\w*_\d+_\w*\.mat` – rock321987 May 23 '16 at 09:00
  • so what if, string starts with the digit and after an underscore and something like [digits]_something.mat. And how should I change it into the patern that I want like filename_%d_something.mat – freezer May 23 '16 at 09:07
  • that's why I asked will there be two underscores? – rock321987 May 23 '16 at 09:09
  • Not sure, It is up to the client. – freezer May 23 '16 at 09:10
  • is there anything in pattern that is fixed? – rock321987 May 23 '16 at 09:12
  • @freezer your comments contradicts, please test the regex provided by rock321987 and find cases where it does not work for you - the regex does "change it into the patern that I want like filename_%d_something.mat" – GameOfThrows May 23 '16 at 09:16
  • Nope. I thought that, if I can compare two filenames and find which chars are different I can and substring to that location. But this time, length of index digit matters. And it takes time to read 100 files. Lets say I have filename_1_something.m and filename_2_something.m matlab can give me that 10th char is different then I replace 2 with %d and everything is OK. But I need to choos two filenames as same length. if I select filename_1_something.m and filename_10_something.m it wont work. Or maybe I need to compare the lenggth of the files also. – freezer May 23 '16 at 09:17
  • reading the filenames take literally 0.0001 second, so you should be able to have all of the filenames under the dir - if you compare the length of the filenames, what if you have a file that just happens to be foreign and breaks? Once you load in all the filenames, you can do whatever `strcmp` you like. – GameOfThrows May 23 '16 at 09:21
  • @GameOfThrows I used regexp given by rock321987, it just gave me the filename, So how should I find the position of the digits and change into %d. Sorry for the lack of knowledge about regexp btw. – freezer May 23 '16 at 09:23
  • regexprep(files(1).name,'(\w*)_(\d+)_(\w*)\.mat','$1_%d_$3') this worked for the case with two underscores before and after the digits. How can it be improved to cover where it begins or ends with the digits? – freezer May 23 '16 at 09:28
  • @freezer add an other regex or if you are only looking for underscores, just use userscore as your regex – GameOfThrows May 23 '16 at 09:33
  • if you can add all your requirements, then it can be solved – rock321987 May 23 '16 at 09:38
  • @GameOfThrows I have check the directory reading time as; for p = 1:length(paths.mesDir) mes = paths.mesDir{p}; tic; files = dir([mes '\*.mat']); toc; end end yields ~11 secs. – freezer May 23 '16 at 09:50
  • the directory reading time should be just dir('*.mat') which is the time needed to create the stuct with all your file names in it, the for loops do not count. – GameOfThrows May 23 '16 at 09:58
  • there are multiple folders. it just runs for one folder. And the 11sec is just for 1 dir() fcn. – freezer May 23 '16 at 10:00
  • dir does subfolders, so no need to loop it for each folder. – GameOfThrows May 23 '16 at 10:02
  • Folders not recursive. It loops over different folders to find file patters for each folders. – freezer May 23 '16 at 10:04
  • I see...well, I don't see any other methods that will go much faster - you might have to bear with the 11 seconds.. – GameOfThrows May 23 '16 at 10:06
  • Thanks. I am very appreciated with your response :) – freezer May 23 '16 at 10:07

1 Answers1

1

using the regex provided by @rock321987 -

 names = dir('*.mat');
 num = length(names);
 expression = '\w*_\d+_\w*\.mat';
 for n = 1:num
    str = names(n).name;
    nameList{n} = regexp(str,expression,'match')
 end

works on:

test_1_something.mat
test_10_something.mat

changing the regex to just \w*_\w*\.mat

works for

test_1.mat
1_test.mat
test_1_something.mat
test_10_something.mat

but also works for anything with an string joined by underscore .mat

GameOfThrows
  • 4,510
  • 2
  • 27
  • 44
  • Thanks, but I needed something to be able to replace the digits with %d two char string. I tried a few expressions and came up with the working one. I hope there will be no error. regexprep('test_100_something.mat','(\w*_|^)(\d+)(_\w*\.mat)','$1%d$3'); – freezer May 23 '16 at 10:01