cppiscute
cppiscute

Reputation: 737

User interactive sorting of files in an order using the GUIDE matlab

I have a GUIDE GUI,

It is having a file upload push button.

Now once the files are uploaded, I have to give the user the oppertunity to order them (like file1 file2 file3...which one should be first and which one should be second and so on...).

My file upload function looks like

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

Now My filesortgui is a pop-up GUI, which lets the user to sort the files however he wants.

This is 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

So I can get the file names into my pop-up GUI.

The list box doesn't lets the user move the string fields. So is there any way I can make the list box string fields to move. or is there any other way the user interactive sorting of files can be done?

Upvotes: 2

Views: 197

Answers (1)

Suever
Suever

Reputation: 65440

There a few ways you can approach this. One common approach is to have buttons to the right of the listbox with "promote" and "demote" titles that will move the currently selected item up or down the list.

Here is some sample code to do just that.

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

That will give you something like this.

enter image description here

The other option is to rely on the underlying Java object and respond to mouse events. Erik Koopmans has a file exchange entry, Reorderable Listbox, that is able to do just that.

Upvotes: 4

Related Questions