使用 GUIDE matlab 按顺序对文件进行用户交互式排序
User interactive sorting of files in an order using the GUIDE matlab
我有一个 GUIDE GUI,
它有一个文件上传按钮。
现在文件上传后,我必须给用户机会来订购它们(比如 file1 file2 file3...哪个应该是第一个,哪个应该是第二个等等...)。
我的文件上传功能是这样的
function pbu_Callback(hObject, eventdata, handles)
[FileName,PathName,FilterIndex] = uigetfile('*.fig' , 'SELECT FILES TO UPLOAD','MultiSelect','on');
output = cellfun(@(x) {horzcat(x)},FileName);
handles.files = output;
guidata(hObject, handles);
filesortgui;
end
现在我的 filesortgui 是一个弹出式 GUI,用户可以根据需要对文件进行排序。
这是filesortgui.m
function filesortgui
S.filesortfigure = figure('units','pixels',...
'position',[600 600 600 400],...
'menubar','none',...
'name','Input Files Sorting',...
'numbertitle','off',...
'resize','off');
hGui = findobj('Tag','fig');
handles = guidata(hGui);
handles.files(1)
handles.files(2)
end
这样我就可以将文件名输入到我的弹出式 GUI 中。
列表框不允许用户移动字符串字段。那么有什么方法可以使列表框字符串字段移动。还是有任何其他方式可以完成用户交互式文件排序?
有几种方法可以解决这个问题。一种常见的方法是在列表框的右侧放置带有 "promote" 和 "demote" 标题的按钮,这些按钮会将当前选定的项目在列表中向上或向下移动。
这里有一些示例代码可以做到这一点。
function reorderlist()
items = {'File1.png', 'File2.png', 'File3.png'};
hfig = figure();
hlist = uicontrol('Parent', hfig, 'style', 'listbox', 'string', items);
set(hlist, 'units', 'norm', 'position', [0 0 0.75 1])
promote = uicontrol('Parent', hfig, 'String', '^');
set(promote, 'units', 'norm', 'position', [0.8 0.75 0.15 0.15])
demote = uicontrol('Parent', hfig, 'String', 'v');
set(demote, 'units', 'norm', 'position', [0.8 0.55 0.15 0.15])
% Set button callbacks
set(promote, 'Callback', @(s,e)moveitem(1))
set(demote, 'Callback', @(s,e)moveitem(-1))
function moveitem(increment)
% Get the existing items and the current item
items = get(hlist, 'string');
current = get(hlist, 'value');
toswap = current - increment;
% Ensure that we aren't already at the top/bottom
if toswap < 1 || toswap > numel(items)
return
end
% Swap the two entries that need to be swapped
inds = [current, toswap];
items(inds) = flipud(items(inds));
% Update the order and the selected item
set(hlist, 'string', items);
set(hlist, 'value', toswap)
end
end
那会给你这样的东西。
另一种选择是依赖底层的 Java object 和 respond to mouse events. Erik Koopmans has a file exchange entry, Reorderable Listbox,它能够做到这一点。
我有一个 GUIDE GUI,
它有一个文件上传按钮。
现在文件上传后,我必须给用户机会来订购它们(比如 file1 file2 file3...哪个应该是第一个,哪个应该是第二个等等...)。
我的文件上传功能是这样的
function pbu_Callback(hObject, eventdata, handles)
[FileName,PathName,FilterIndex] = uigetfile('*.fig' , 'SELECT FILES TO UPLOAD','MultiSelect','on');
output = cellfun(@(x) {horzcat(x)},FileName);
handles.files = output;
guidata(hObject, handles);
filesortgui;
end
现在我的 filesortgui 是一个弹出式 GUI,用户可以根据需要对文件进行排序。
这是filesortgui.m
function filesortgui
S.filesortfigure = figure('units','pixels',...
'position',[600 600 600 400],...
'menubar','none',...
'name','Input Files Sorting',...
'numbertitle','off',...
'resize','off');
hGui = findobj('Tag','fig');
handles = guidata(hGui);
handles.files(1)
handles.files(2)
end
这样我就可以将文件名输入到我的弹出式 GUI 中。
列表框不允许用户移动字符串字段。那么有什么方法可以使列表框字符串字段移动。还是有任何其他方式可以完成用户交互式文件排序?
有几种方法可以解决这个问题。一种常见的方法是在列表框的右侧放置带有 "promote" 和 "demote" 标题的按钮,这些按钮会将当前选定的项目在列表中向上或向下移动。
这里有一些示例代码可以做到这一点。
function reorderlist()
items = {'File1.png', 'File2.png', 'File3.png'};
hfig = figure();
hlist = uicontrol('Parent', hfig, 'style', 'listbox', 'string', items);
set(hlist, 'units', 'norm', 'position', [0 0 0.75 1])
promote = uicontrol('Parent', hfig, 'String', '^');
set(promote, 'units', 'norm', 'position', [0.8 0.75 0.15 0.15])
demote = uicontrol('Parent', hfig, 'String', 'v');
set(demote, 'units', 'norm', 'position', [0.8 0.55 0.15 0.15])
% Set button callbacks
set(promote, 'Callback', @(s,e)moveitem(1))
set(demote, 'Callback', @(s,e)moveitem(-1))
function moveitem(increment)
% Get the existing items and the current item
items = get(hlist, 'string');
current = get(hlist, 'value');
toswap = current - increment;
% Ensure that we aren't already at the top/bottom
if toswap < 1 || toswap > numel(items)
return
end
% Swap the two entries that need to be swapped
inds = [current, toswap];
items(inds) = flipud(items(inds));
% Update the order and the selected item
set(hlist, 'string', items);
set(hlist, 'value', toswap)
end
end
那会给你这样的东西。
另一种选择是依赖底层的 Java object 和 respond to mouse events. Erik Koopmans has a file exchange entry, Reorderable Listbox,它能够做到这一点。