how to sort push button

1 visualización (últimos 30 días)
win
win el 17 de Jul. de 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

Respuesta aceptada

Jan
Jan el 18 de Jul. de 2013
Editada: Jan el 18 de Jul. de 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 comentarios
win
win el 20 de Jul. de 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
Jan el 20 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 17 de Jul. de 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 comentarios
Jan
Jan el 17 de Jul. de 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
win el 18 de Jul. de 2013
Editada: win el 18 de Jul. de 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
imshow(Img)
addpath('Code')
dir_name = 'Code';
top_dir=dir(dir_name);
i = 0;j = 0;count =0;
for m=1:length(top_dir)
tsize = length(top_dir)-2;
tsize = tsize/9;
name=top_dir(m,1).name;
if (strcmp(name,'.')||strcmp(name,'..'))
else
dir1=dir([dir_name,'\',name]);
if i<0.98
hbsp = uicontrol('Parent',h1,'String',char(name),...
'Units','normalized','FontSize',8.5,...
'Position',[.01+i .80-j .10 .15],'Callback',@types);
i = i + 0.11;
count = count +1;
end
if count>8
j = j + 0.2;
i = 0;
count = 0;
end
end
end
end
function types(obj,event)
if fun_cnt == 1
delete(hsp)
hsp = [];
end
ptr = get(obj,'String');
k = 0;ite=1;l = 0;count1 = 0;
dir1 = dir([dir_name,'\',char(ptr)]);
for m1=1:length(dir1)
name1=dir1(m1,1).name;
Name1 = name1;
if (strcmp(name1,'.')||strcmp(name1,'..'))
else
if k<0.98
hsp(ite) = uicontrol('Parent',h2,'String',char(Name1(1:end)),...
'Units','normalized','FontSize',8.5,...
'Position',[.01+k .85-l .3 .1],'callback',@code_upload);
k = k + 0.33;
count1 = count1 +1;
ite = ite+1;
end
if count1>2
l = l + 0.12;
k = 0;
count1 = 0;
end
end
end
end
this is the code

Iniciar sesión para comentar.


win
win el 18 de Jul. de 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);
end

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by