【问题标题】:Matlab guide: Adding / deleting items from listboxMatlab 指南:从列表框中添加/删除项目
【发布时间】:2018-03-16 21:27:30
【问题描述】:

我正在尝试创建一个可以动态添加或删除项目的列表框。
设置如下所示:

不幸的是——从图片中可以看出——当我删除元素时,列表的总长度保持不变,而不是缩小列表,显示的列表现在包含孔。

有谁知道如何避免这种行为?

到目前为止,这是我删除按钮的代码:

function btnDeleteLabel_Callback(hObject, eventdata, handles)
    selectedId = get(handles.listbox_labels, 'Value');        % get id of selectedLabelName
    existingItems = get(handles.listbox_labels, 'String');    % get current listbox list
    existingItems{selectedId} = [];                   % delete the id
    set(handles.listbox_labels, 'String', existingItems);     % restore cropped version of label list

【问题讨论】:

    标签: matlab listbox matlab-guide


    【解决方案1】:

    删除“空”条目的最简单方法是使用剩余的项目更新listbox 字符串。

    有三种可能:

    • 第一个元素已被删除:新列表将是upd_list={existingItems{2:end}}
    • 最后一个元素已被删除:新列表将是upd_list={existingItems{1:end-1}}
    • 一个中间元素已被删除:新列表将是upd_list={existingItems{1:selectedId-1} existingItems{selectedId+1:end}}

    您还可以检查列表中的所有元素是否已被删除,在这种情况下,禁用“删除”pushbutton;在这种情况下,您必须在“添加”callback 中启用它。

    btnDeleteLabel_Callback 的可能实现可能是:

    % --- Executes on button press in btnDeleteLabel.
    function btnDeleteLabel_Callback(hObject, eventdata, handles)
    % hObject    handle to btnDeleteLabel (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    selectedId = get(handles.listbox_labels, 'Value')        % get id of selectedLabelName
    existingItems = get(handles.listbox_labels, 'String')    % get current listbox list
    %
    % It is not necessary
    %
    % existingItems{selectedId} = []                   % delete the id
    
    % Identify the items: if in the list only one ites has been added the
    % returned list is a char array
    if(class(existingItems) == 'char')
       upd_list=''
       set(handles.listbox_labels, 'String', upd_list)
    else
       % If the returned list is a cell array there are three cases
       n_items=length(existingItems)
       if(selectedId == 1)
          % The first element has been selected
          upd_list={existingItems{2:end}}
       elseif(selectedId == n_items)
          % The last element has been selected
          upd_list={existingItems{1:end-1}}
          % Set the "Value" property to the previous element
          set(handles.listbox_labels, 'Value', selectedId-1)
       else
          % And element in the list has been selected
          upd_list={existingItems{1:selectedId-1} existingItems{selectedId+1:end}}
       end
    end
    % Update the list
    set(handles.listbox_labels, 'String', upd_list)     % restore cropped version of label list
    
    % Disable the delete pushbutton if there are no more items
    existingItems = get(handles.listbox_labels, 'String')
    if(isempty(existingItems))
       handles.btnDeleteLabel.Enable='off'
    end
    

    希望这会有所帮助,

    卡普拉'

    【讨论】:

      【解决方案2】:

      只需将单元格括号替换为普通括号即可:

      %existingItems{selectedId} = []; % replace this with next line
      existingItems(selectedId) = [];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-07
        • 2010-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多