MATLAB Answers

m b

How do I sort filenames base on the numbers right before file name in MATLAB?

m b
さんによって質問されました 2019 年 5 月 26 日
最新アクティビティ m b
さんによって コメントされました 2019 年 5 月 27 日
I have file names containing everything, but before the file format (like .mat), there is always a number indicating trials. how can I sort it only base on the trial number before the file format ''.mat''.
here are some examples of my files: S202T4_19112018_MS_T-_21.mat , S202T4_19112018_MS_stay_1.mat

  3 件のコメント

2019 年 5 月 26 日
So you want to sort a cell array, each containing a char array with a name, to be sorted based on the number between the _ and .mat?
m b
2019 年 5 月 27 日
Yes exactly. those are my trial numbers that need to be sorted.
the strings before those numbers are not the same for each file, that's why I need something depends on the numbers before the file format (here .mat).
2019 年 5 月 27 日
You can adapt the extract_numbers function to fit any pattern. You can use fileparts to remove the extension and then strfind to find the final underscore. The general structure of my answer should still stand.

サインイン to comment.


1 件の回答

回答者: Rik
2019 年 5 月 27 日

This should to the trick. Note that this will return an error if there is no match.
function val=extract_number(str)

  1 件のコメント

m b
2019 年 5 月 27 日
Thanks, It works very well!!!

サインイン to comment.

Translated by