How to select a file based on the filename?

Hi, I have several files and I want to just select one based on the maximum number which I could find on the file name. For example I have
abcEFG_1all_0.0675_0.2035.mat
abcEFG_1all_0.087534_0.40354.mat
abcEFG_1all_0.167534_0.603123.mat
and I just want to select and work on this file
abcEFG_1all_0.167534_0.603123.mat
because the numbers are higher than the others. Any suggestion would be appreciated in advance.

 Accepted Answer

7 Comments

Thanks Walter for your quick response unfortunately,
Site Temporarily Unavailable
Due to planned maintenance, our site is currently unavailable. Please come back later.
Thanks
Is the "0." part of the number?
Is only the first number of the two to be paid attention to? Or are both numbers in the file name to be paid attention to?
yes 0. is a number .Actually the first one would be much more important.Thanks Walter
dinfo = dir('abc*.mat');
filenames = {dinfo.name};
parts = regexp(filenames, '_', 'split');
part3 = cellfun(@(C) C{3}, parts, 'Uniform', 0);
val3 = str2double(part3);
[maxval3, maxidx3] = max(val3);
desired_file = filenames{maxidx3};
Thanks a lot.You are amazing!!!
Hi Walter, I stuck in this script I followed your answer but for this one I get the error.I really would appreciate if you could solve my problem. I have
net1_0.2327.mat
net1_0.3425.mat
net2_0.8765.mat
net2_0.6754.mat ,......to net50_0.87654.mat
I would like to get these net1_0.3425.mat net2_0.8765.mat ,.... the max of each net1 to net net50(comparing net1 with net1 and net2 with net2 and so on) I used your script
dinfo = dir('net*.mat');
filenames = {dinfo.name};
parts = regexp(filenames, '_', 'split');
part2 = cellfun(@(C) C{2}, parts, 'Uniform',0);
parts3 = regexp(part2, '.mat', 'split');% to remove mat extension
val2 = str2double(parts3)% I get NaN instead of numbers!
Thanks in advance for your help.
Answered in the new Question you opened about this.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!