Effacer les filtres
Effacer les filtres

how to sort push button

1 vue (au cours des 30 derniers jours)
win le 17 Juil 2013
i have 10 number of folders. when i run the main.m file the folders shows like 10 number of push buttons. the push button having the names of that folder. now i need to sort the push buttons by folder created date, please help me. thanks in advance

Réponse acceptée

Jan le 18 Juil 2013
Modifié(e) : Jan le 18 Juil 2013
I assume, you want:
top_dir = dir(dir_name);
[dummy, index] = sort([top_dir.datenum]);
top_dir = top_dir(index);
Now the files and folders inside the struct top_dir are chronologically sorted.
Working with relative path names is dangerous. Matlab tries to be smart and searches in all folders of the Matlab path. Better use absolute paths and avoid to append a data folder to the Matlab path:
currentPath = cd;
dir_name = fullfile(currentPath, 'Code');
top_dir = dir(dir_name);
fullfile is smarter than hardcoding the file separator: 1. it considers existing separators, e.g. in "C:\", and it cares about the preferences of the operating system: \ or /
  2 commentaires
win le 20 Juil 2013
i think its used to sort the directory. but i need to sort the push buttons in accordance with the folder creation date. can i use the above code for my condition? will it work?
Jan le 20 Juil 2013
@godwin: The code I have posted sorts the names of the folder according to the dates. Afterwards you create the buttons. And because the names are sorted already, the buttons have the same order also. So there is no reason to sort the buttons. It is not even meaningful to sort the buttons, when the contents of the buttons are sorted already.
Btw. char(Name1(1:end)) can be simplified to Name1.

Connectez-vous pour commenter.

Plus de réponses (2)

Jan le 17 Juil 2013
  1. Obtain the creation date of the folders by dir. The replied field datenum is the best value to sort for.
  2. Let sort find the wanted order.
  3. Set the strings according to this order.
If you need a more explicit implementation, show us, what you have done so far and ask more explicitly for a specific problem.
  3 commentaires
Jan le 17 Juil 2013
Where did you get these names from? I guess you use the DIR command, and then you can use the .datenum field directly for sorting also. Please provide the relevant part of the code, because we cannot guess how you create this list of names.
win le 18 Juil 2013
Modifié(e) : win le 18 Juil 2013
if true
%%Method Panel Design
f = figure('Visible','on','NumberTitle','off','Resize','on','Name','Matlab Automation',...
'MenuBar','none', ...
'Toolbar','none', ...
'Position',[0,0,750,1500]);% Figure
h = uipanel('Parent',f); %Parent panel
h1 = uipanel('Parent',h,'Title','methods','FontSize',12,...
'Position',[0.01 0.70 0.98 .3] ); % Method Panel
h2 = uipanel('Parent',h,'Title','Types','FontSize',12,...
'Position',[0.01 0.1 0.33 .6] ); % Types Panel
h3 = uipanel('Parent',h,'Title','Results','FontSize',12,...
'Position',[0.345 0.1 0.645 .6] ); % Results Panel
Img = imread('1.jpg');
axes('Parent',f,'Position',[0.35 0.4 0.2 .3])
axis off
dir_name = 'Code';
i = 0;j = 0;count =0;
for m=1:length(top_dir)
tsize = length(top_dir)-2;
tsize = tsize/9;
if (strcmp(name,'.')||strcmp(name,'..'))
if i<0.98
hbsp = uicontrol('Parent',h1,'String',char(name),...
'Position',[.01+i .80-j .10 .15],'Callback',@types);
i = i + 0.11;
count = count +1;
if count>8
j = j + 0.2;
i = 0;
count = 0;
function types(obj,event)
if fun_cnt == 1
hsp = [];
ptr = get(obj,'String');
k = 0;ite=1;l = 0;count1 = 0;
dir1 = dir([dir_name,'\',char(ptr)]);
for m1=1:length(dir1)
Name1 = name1;
if (strcmp(name1,'.')||strcmp(name1,'..'))
if k<0.98
hsp(ite) = uicontrol('Parent',h2,'String',char(Name1(1:end)),...
'Position',[.01+k .85-l .3 .1],'callback',@code_upload);
k = k + 0.33;
count1 = count1 +1;
ite = ite+1;
if count1>2
l = l + 0.12;
k = 0;
count1 = 0;
this is the code

Connectez-vous pour commenter.

win le 18 Juil 2013
i think this is the answer i found another site after line 29 add this code. i think it will help for others too.
if true
%%sort push buttons
Afields = fieldnames(top_di);
Acell = struct2cell(top_di);
sz = size(Acell);
Acell = reshape(Acell, sz(1), []);
Acell = Acell';
Acell = sortrows(Acell, 2);
Acell = reshape(Acell', sz);
top_dir = cell2struct(Acell, Afields, 1);


En savoir plus sur File Operations dans Help Center et File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by