How to sort uigetfile multiselect data output (cell datas) correctly?

17 vues (au cours des 30 derniers jours)
Tyann Hardyn
Tyann Hardyn le 16 Oct 2021
Commenté : Pratyush Swain le 21 Mar 2025
Hi, Community
I want to sort filename from uigetfile (multiselect) so that each file would be processed in sequence by that sort later...
Iam creating an uigetfile function like this :
[namafile,arah]=uigetfile({'*.txt', 'Text-Files (*.txt)'},'Load Extraction Data File', 'Multiselect','on');
full_ekstrak = fullfile(arah, namafile); %I dont know where is between these datas that can be used to sort each of filename data
nfiles = numel(full_ekstrak); %I dont know where is between these datas that can be used to sort each of filename data
namafiles_ekstrak = cellstr(sort(namafile)); %I dont know where is between these datas that can be used to sort each of filename data
f = cellstr(fullfile(arah,namafile)); %I dont know where is between these datas that can be used to sort each of filename data
file = length(f); %I dont know where is between these datas that can be used to sort each of filename data
and one of the output variable would be like this (multiselect data) :
namafiles_ekstrak'
ans =
3×1 cell array
{'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Nov-2017 Hingga 30-Nov-2017 Stasiun TUN.txt'}
{'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Oct-2017 Hingga 31-Oct-2017 Stasiun TUN.txt'}
{'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Sep-2017 Hingga 30-Sep-2017 Stasiun TUN.txt'}
Thats Not what i want to get from namafiles_ekstrak variable.
I just want to sort it as ;
{'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Sep-2017 Hingga 30-Sep-2017 Stasiun TUN.txt'}
{'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Oct-2017 Hingga 31-Oct-2017 Stasiun TUN.txt'}
{'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Nov-2017 Hingga 30-Nov-2017 Stasiun TUN.txt'}
So it would be sorted by the datetime IN each of cell output (cell matrix) like that.....
Would it be possible to sort cell matrix like that. I know, maybe its only a small problem and easy, BUT Anyone, please help me in finding this solution that is sooo difficult for me. Iam so grateful if anyone can help me out. Thank you so much /.\ /.\ /.\

Réponses (1)

Pratyush Swain
Pratyush Swain le 20 Mar 2025
Hi Tyann,
To sort the filenames based on the dates embedded within them, you can extract the date information from each filename, convert it to a date object, and then sort based on these date objects.
% Demo namafile (I borrowed it from the question itself) %
namafile = {
'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Nov-2017 Hingga 30-Nov-2017 Stasiun TUN.txt',
'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Oct-2017 Hingga 31-Oct-2017 Stasiun TUN.txt',
'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Sep-2017 Hingga 30-Sep-2017 Stasiun TUN.txt'
};
% Extract dates and convert them to datetime objects
dates = cellfun(@(x) extractBetween(x, 'Periode ', ' Hingga'), namafile, 'UniformOutput', false);
dates = cellfun(@(x) datetime(x, 'InputFormat', 'dd-MMM-yyyy'), dates);
% Sort the dates and get the sorting order
[~, sortIdx] = sort(dates);
% Apply the sorting order to the filenames
sorted_namafile = namafile(sortIdx);
% Display sorted filenames
namafiles_ekstrak = sorted_namafile
namafiles_ekstrak = 3x1 cell array
{'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Sep-2017 Hingga 30-Sep-2017 Stasiun TUN.txt'} {'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Oct-2017 Hingga 31-Oct-2017 Stasiun TUN.txt'} {'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Nov-2017 Hingga 30-Nov-2017 Stasiun TUN.txt'}
For more information on usage of 'extractBetween' and 'dateTime' function please refer to:
Hope this helps.
  2 commentaires
Voss
Voss le 20 Mar 2025
No need to use cellfun for that:
% Demo namafile (I borrowed it from the question itself) %
namafile = {
'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Nov-2017 Hingga 30-Nov-2017 Stasiun TUN.txt',
'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Oct-2017 Hingga 31-Oct-2017 Stasiun TUN.txt',
'Komponen Magnet Data Definitif IAGA Menitan (dmin) Periode 01-Sep-2017 Hingga 30-Sep-2017 Stasiun TUN.txt'
};
% Extract dates and convert them to datetime objects
dates = datetime(extractBetween(namafile,'Periode ',' Hingga'),'InputFormat','dd-MMM-yyyy')
dates = 3x1 datetime array
01-Nov-2017 01-Oct-2017 01-Sep-2017
Pratyush Swain
Pratyush Swain le 21 Mar 2025
Thanks Voss, this is better

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by