⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addremovelist.m

📁 使用MATLAB编写的GUI程序 可实现list菜单的增减
💻 M
📖 第 1 页 / 共 2 页
字号:
                        error('The %s parameter value must be a cell array of strings of length 2',parName)
                    else
                        set(handles.leftListName,'String',parVal{1})
                        set(handles.rightListName,'String',parVal{2})
                    end
                case 4 % Window title
                    if ~ischar(parVal)
                        error('The %s parameter value must be a string',parName)
                    else
                        set(handles.mainFigure,'Name',parVal)
                    end
                case 5 % Multiselection
                    if ~islogical(parVal)
                        error('The %s parameter value must be either true or false',parName)
                    else
                        if parVal
                            set(handles.allowMultiSelect,'Value',1)
                            handles.multi=true;
                        else
                            set(handles.allowMultiSelect,'Value',0)
                            handles.multi=false;
                        end
                    end
                case 6 % Keep contents and corresponding indices sorted
                    if ~islogical(parVal)
                        error('The %s parameter value must be either true or false',parName)
                    else
                        if parVal
                            set(handles.sortLists,'Value',1)
                            handles.sort=true;
                            handles.rightListContents=sort(handles.rightListContents);
                            handles.leftListContents=sort(handles.leftListContents);
                        else
                            set(handles.sortLists,'Value',0)
                            handles.sort=false;
                        end
                    end
                    
            end
        end
    end
end

% Initialize the listboxes (fill) and the indices of items
set(handles.leftList,'String',handles.leftListContents,'Value',1)
if handles.multi
    set(handles.leftList,'Max',length(handles.leftListContents))
end
if ~isempty(handles.rightListContents)
    set(handles.rightList,'String',handles.rightListContents,'Value',1)
    set(handles.removeElement,'Enable','on')
    if handles.multi
        set(handles.rightList,'Max',length(handles.rightListContents))
    end
    handles.indflag=true;
end
handles.leftIndices=1:length(handles.leftListContents);
handles.rightIndices=[];

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes addremovelist wait for user response (see UIRESUME)
uiwait(handles.mainFigure);


% --- Outputs from this function are returned to the command line.
function varargout = addremovelist_OutputFcn(hObject, eventdata, handles) 

% Handles button press
if (get(handles.cancelButton,'Value')==0)
    delete(handles.mainFigure);
elseif (get(handles.okButton,'Value')==0)
    delete(handles.mainFigure);
end

% Get default command line output from handles structure
varargout{1}=handles.rightContents;
varargout{2}=handles.leftContents;
if ~handles.indflag
    varargout{3}=handles.rightIndices;
    varargout{4}=handles.leftIndices;
end


%%%%%%%%%%%%%%%%%%% BEGIN LISTBOXES %%%%%%%%%%%%%%%%%%%%

% --- Executes on selection change in leftList.
function leftList_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function leftList_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in rightList.
function rightList_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function rightList_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%%%%%%%%%%%%%%%%%% END LISTBOXES %%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%% BEGIN LISTBOX CONTROL BUTTONS %%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in addElement.
function addElement_Callback(hObject, eventdata, handles)

% Determine what remains and what is to be added
leftNames=get(handles.leftList,'String');
leftToAddInd=get(handles.leftList,'Value');
leftToAddNames=leftNames(leftToAddInd);

% Do the job
oldstr=get(handles.rightList,'String');
addstr=leftToAddNames;
verify=ismember(addstr,oldstr);
if isempty(oldstr)
    newstr=[oldstr;addstr];
elseif length(verify)==1
    if ~ismember(addstr,oldstr)
        newstr=[oldstr;addstr];
    else
        newstr=oldstr;
    end
else
    newstr=[oldstr;addstr];
end

% Update the right list...
set(handles.rightList,'String',newstr)
% Sort it if wanted
if handles.sort
    neweststr=sort(newstr);
    set(handles.rightList,'String',neweststr)
end
% Handle multiple selection possibility
if handles.multi
    set(handles.rightList,'Max',length(newstr))
end

% Update the left list by removing elements
remaining=leftNames;
remaining(leftToAddInd)=[];
set(handles.leftList,'String',remaining,'Value',1)
% Sort it if wanted
if handles.sort
    [remainingnew,remainind]=sort(remaining);
    set(handles.leftList,'String',remainingnew,'Value',1)
end
% Handle multiple selection possibility
if handles.multi
    set(handles.leftList,'Max',length(remaining))
end

% Fix the final indices vectors
if ~handles.indflag
    if ~handles.sort
        newindices=handles.leftIndices(leftToAddInd);
        handles.leftIndices(leftToAddInd)=[];
        handles.rightIndices=[handles.rightIndices,newindices];
    else
        newindices=handles.leftIndices(leftToAddInd);
        handles.leftIndices(leftToAddInd)=[];
        handles.leftIndices=handles.leftIndices(remainind);
        handles.rightIndices=sort([handles.rightIndices,newindices]);
    end
end

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % if size(handles.leftIndices,1)>size(handles.leftIndices,2)
% %     handles.leftIndices=handles.leftIndices';
% %     handles.rightIndices=handles.rightIndices';
% % end
% msgleft=['The left indices are : ',num2str(handles.leftIndices)];
% msgright=['The right indices are : ',num2str(handles.rightIndices)];
% disp(msgleft)
% disp(msgright)
% disp(' ')
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Manage Add/Remove buttons
if ~isempty(get(handles.rightList,'String'))
    set(handles.removeElement,'Enable','on')
end
if isempty(get(handles.leftList,'String'))
    set(handles.addElement,'Enable','off')
end

% Update the output strings
handles.leftContents=get(handles.leftList,'String');
handles.rightContents=get(handles.rightList,'String');
guidata(hObject,handles);


% --- Executes on button press in removeElement.
function removeElement_Callback(hObject, eventdata, handles)

% Determine what remains and what is to be added
rightNames=get(handles.rightList,'String');
rightToRemoveInd=get(handles.rightList,'Value');
rightToRemoveNames=rightNames(rightToRemoveInd);

% Do the job
oldstr=get(handles.leftList,'String');
addstr=rightToRemoveNames;
verify=ismember(addstr,oldstr);
if isempty(oldstr)
    newstr=[oldstr;addstr];
elseif length(verify)==1
    if ~ismember(addstr,oldstr)
        newstr=[oldstr;addstr];
    else
        newstr=oldstr;
    end
else
    newstr=[oldstr;addstr];
end

% Update the reft list...
set(handles.leftList,'String',newstr)
% Sort it if wanted
if handles.sort
    neweststr=sort(newstr);
    set(handles.leftList,'String',neweststr)
end
% Handle multiple selection possibility
if handles.multi
    set(handles.leftList,'Max',length(newstr))
end

% Update the right list by removing elements
remaining=rightNames;
remaining(rightToRemoveInd)=[];
set(handles.rightList,'String',remaining,'Value',1)
% Sort it if wanted
if handles.sort
    [remainingnew,remainind]=sort(remaining);
    set(handles.rightList,'String',remainingnew,'Value',1)
end
% Handle multiple selection possibility
if handles.multi
    set(handles.rightList,'Max',length(remaining))
end

% Fix the final indices vectors
if ~handles.indflag
    if ~handles.sort
        newindices=handles.rightIndices(rightToRemoveInd);
        handles.rightIndices(rightToRemoveInd)=[];
        handles.leftIndices=[handles.leftIndices,newindices];
    else
        newindices=handles.rightIndices(rightToRemoveInd);
        handles.rightIndices(rightToRemoveInd)=[];
        handles.rightIndices=handles.rightIndices(remainind);
        handles.leftIndices=sort([handles.leftIndices,newindices]);
    end
end

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % if size(handles.leftIndices,1)>size(handles.leftIndices,2)
% %     handles.leftIndices=handles.leftIndices';
% %     handles.rightIndices=handles.rightIndices';
% % end
% msgleft=['The left indices are : ',num2str(handles.leftIndices)];
% msgright=['The right indices are : ',num2str(handles.rightIndices)];
% disp(msgleft)
% disp(msgright)
% disp(' ')
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Manage Add/Remove buttons
if ~isempty(get(handles.leftList,'String'))
    set(handles.addElement,'Enable','on')
end
if isempty(get(handles.rightList,'String'))
    set(handles.removeElement,'Enable','off')
end

% Update the output strings
handles.leftContents=get(handles.leftList,'String');
handles.rightContents=get(handles.rightList,'String');
guidata(hObject,handles);

%%%%%%%%%%%%%%%%%% BEGIN LISTBOX CONTROL BUTTONS %%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%% BEGIN OPTIONS PANEL %%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in allowMultiSelect.
function allowMultiSelect_Callback(hObject, eventdata, handles)

if get(hObject,'Value')==1
    % Multiple selection allowed, maximum number of selections to length of contents
    handles.multi=true;
    set(handles.leftList,'Max',length(handles.leftContents))
else
    % Mutlple selection not allowed, current selection becomes the first item of possibly
    % a vector of selected items
    handles.multi=false;
    vals=get(handles.leftList,'Value');
    set(handles.leftList,'Value',vals(1),'Max',1)
end
guidata(hObject,handles);


% --- Executes on button press in sortLists.
function sortLists_Callback(hObject, eventdata, handles)

if get(hObject,'Value')==1
    handles.sort=true;
else
    handles.sort=false;
end
guidata(hObject,handles);

%%%%%%%%%%%%%%%%%% END OPTIONS PANEL %%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%% BEGIN CLOSE BUTTONS %%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in okButton.
function okButton_Callback(hObject, eventdata, handles)

uiresume(handles.mainFigure);


% --- Executes on button press in cancelButton.
function cancelButton_Callback(hObject, eventdata, handles)

% Return default values
handles.leftContents=handles.leftListContents;
handles.rightContents=handles.rightListContents;
if ~handles.indflag
    handles.leftIndices=1:length(handles.leftListContents);
    handles.rightIndices=[];
end
guidata(hObject,handles);
uiresume(handles.mainFigure);

%%%%%%%%%%%%%%%%%% END CLOSE BUTTONS %%%%%%%%%%%%%%%%%%%%

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -