pop up menu regarding
4 次查看(过去 30 天)
显示 更早的评论
hello,
how can i select a file from the pop up menu?
eg:, I currently choose the .mat file via push bottom, which I think is a bit complicated.
I want to select a .mat file that is stored in a folder via a pop up menu (the .mat file is already in one folder), to make it easier to see
what code should i use?
Thank you
回答(1 个)
Voss
2022-6-11
function file_name = get_mat_file()
h = struct();
h.fig = figure( ...
'Units','pixels', ...
'NumberTitle','off', ...
'IntegerHandle','off', ...
'HandleVisibility','off', ...
'DockControls','off', ...
'Toolbar','none', ...
'Menubar','none', ...
'Name','Select File', ...
'WindowStyle','modal');
h.text_dir = uicontrol( ...
'Parent',h.fig, ...
'Style','text', ...
'Units','pixels', ...
'String','Folder:', ...
'HorizontalAlignment','right');
h.edit_dir = uicontrol( ...
'Parent',h.fig, ...
'Style','edit', ...
'Units','pixels', ...
'String','', ...
'HorizontalAlignment','left', ...
'Callback',@cb_dir);
h.pb_dir = uicontrol( ...
'Parent',h.fig, ...
'Style','pushbutton', ...
'Units','pixels', ...
'String','...', ...
'Callback',@cb_dir);
h.text_file = uicontrol( ...
'Parent',h.fig, ...
'Style','text', ...
'Units','pixels', ...
'String','File:', ...
'HorizontalAlignment','right');
h.popup_file = uicontrol( ...
'Parent',h.fig, ...
'Style','popupmenu', ...
'Units','pixels', ...
'String',{});
h.pb_ok = uicontrol( ...
'Parent',h.fig, ...
'Style','pushbutton', ...
'Units','pixels', ...
'String','OK', ...
'Callback',@cb_ok);
set(h.fig,'SizeChangedFcn',@scf);
fpos = get(h.fig,'Position');
set(h.fig,'Position',[fpos([1 2]) 350 150]);
file_name = '';
is_older_than_isfolder = verLessThan('matlab','9.3');
update_ui();
uiwait(h.fig);
function scf(~,~)
pos = get(h.fig,'Position');
set(h.text_dir,'Position',[10 pos(4)-29 60 18]);
set(h.edit_dir,'Position',[72 pos(4)-30 max(0,pos(3)-104) 22]);
set(h.pb_dir,'Position',[pos(3)-28 pos(4)-30 18 22]);
set(h.text_file,'Position',[10 pos(4)-59 60 18]);
set(h.popup_file,'Position',[72 pos(4)-60 max(0,pos(3)-82) 22]);
set(h.pb_ok,'Position',[pos(3)-75 8 65 26]);
end
function cb_dir(src,~)
if src == h.pb_dir
pn = uigetdir( ...
get(h.edit_dir,'String'), ...
'Select Folder');
if isequal(pn,0)
return
end
set(h.edit_dir,'String',pn);
end
update_ui();
end
function update_ui()
set(h.pb_ok,'Enable','off');
pn = get(h.edit_dir,'String');
if (is_older_than_isfolder && ~exist(pn,'dir')) ...
|| (~is_older_than_isfolder && ~isfolder(pn))
set(h.popup_file,'String',{'< Folder Does not Exist >'},'Value',1);
return
end
files = dir(fullfile(pn,'*.mat'));
if isempty(files)
set(h.popup_file,'String',{'< No mat Files >'},'Value',1);
return
end
set(h.popup_file,'String',{files.name},'Value',1);
set(h.pb_ok,'Enable','on');
end
function cb_ok(~,~)
str = get(h.popup_file,'String');
file_name = fullfile( ...
get(h.edit_dir,'String'), ...
str{get(h.popup_file,'Value')});
delete(h.fig);
end
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!