如何通过GUI将项目添加到列表框

时间:2015-09-15 13:51:34

标签: matlab user-interface listbox uicontrol

我正在拼命尝试为我的GUI创建一个日志框。 我想做的是开始,在按下PUSH按钮后在列表中写入文本。 PUSH按钮的回调功能是:

function run__Callback(hObject, eventdata, handles)

   initial_name=cellstr(get(handles.logbox1,'String'))
    handles.names={'test','haus', 'top', 'down', 'optimistic'}
    handles.names{end,end}=[]                                   %Add Element for new text 
    handles.names{end,end}='neuuu'                              %Add Element
    num_element=length(handles.names)                           %Count Elements
    set(handles.logbox1,'String',handles.names)                 %Aktualisiere Liste

    set(handles.logbox1,'Top',num_element)                      %Set Listbox to the last Element

并且列表框位于同一GUI中。然而,有一个错误:

rror使用hg.uicontrol / set

The name 'Top' is not an accessible property for an instance of class 'uicontrol'.

任何人都可以帮助我,我不明白什么是错的?

最好的问候,约翰

1 个答案:

答案 0 :(得分:1)

您收到错误是因为Top不是property的{​​{1}};此外,listbox uicontrol不是任何Top的属性。

您可以找到here the list of uicontrol properties.

靠近“顶部”的最多列表框属性是uicontrol

我创建了两个简单的GUI,可以帮助您管理列表框的访问。

主GUI“add_to_listbox”包含:

  • ListboxTop listbox listbox1
  • tag pushbutton按钮1,tag“添加操作”:每次用户按下时,都会添加一个字符串,如“主GUI:插入的字符串#x”列表框的顶部(“x”是一个conter)
  • String pushbutton“open_subgui”,tag“打开SubGUI”打开第二个GUI

SubGUI(“add_to_listbox_subgui”)包含

  • String pushbutton按钮1,tag“在主GUI上添加操作”:每次用户按下它时,字符串如“SUB GUI Inserted string #x”为添加在主GUI列表框之上(“x”是一个conter)

SubGUI通过使用存储在主GUI String中的主GUi listbox handle来处理向主GUI列表框添加字符串(当主GUI打开SubGUI时,它接收到作为输入guidata到主GUI;通过它输入主GUI handle)。

以下您可以找到:

  • 初始化
  • 中的“操作计数器”的主GUI guidata
  • 每次按下OpeningFcn时,在列表框中添加字符串的主GUI pushbutton1_Callback
  • 打开SubGUI的主GUI pushbutton
  • 处理SubGUI和主GUI open_subgui_Callback的SubGUI OpeningFcn
  • SubGUI guidata每次按pushbutton1_Callback时在主GUI列表框中添加一个字符串

主GUI pushbutton

OpeningFcn

主GUI % --- Executes just before add_to_listbox is made visible. function add_to_listbox_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to add_to_listbox (see VARARGIN) % Choose default command line output for add_to_listbox handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes add_to_listbox wait for user response (see UIRESUME) % uiwait(handles.figure1); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Get Main GUI guidata gui_data=guidata(gcf); % Add (and initialize) button action counter to Main GUI guidata gui_data.btn_action=0; % Set Main GUI guidata guidata(gcf,gui_data); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %

pushbutton1_Callback

主GUI % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Add button action (from Main GUI) string to the listbox % % Get Mani GUI guidata gui_data=guidata(gcf); % Increment button action counter gui_data.btn_action=gui_data.btn_action+1; % Set Main GUI guidata (to store button action counter) guidata(gcf,gui_data); % Generate action string and add it to the listbox % The firt strimg is directly add to the listbox if(gui_data.btn_action == 1) new_str='Main GUI: Inserted string #1'; set(handles.listbox1,'string',new_str); else new_str=['Main GUI: Inserted string #' num2str(gui_data.btn_action)]; % The fisrt string in the list box is returned as "string", to add the % second one, it has has to be first converted into a cellarray if(gui_data.btn_action == 2) tmp_str=cellstr(get(handles.listbox1,'string')); else % The order of the string in the listbox is reversed to have the last % one on top tmp_str=flipud(get(handles.listbox1,'string')); end % Set the updated set of seting to the listbox tmp_str{end+1,1}=new_str; set(handles.listbox1,'string',flipud(tmp_str)); end

open_subgui_Callback

SubGUI % --- Executes on button press in open_subgui. function open_subgui_Callback(hObject, eventdata, handles) % hObject handle to open_subgui (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Open the Sub GUI; the handle of the Main GUI is passed as argument to % This allows Sub GUI accessing to the Main GUI guidata add_to_listbox_subgui(gcf) % Disable the "Open Sub GUI" button set(handles.open_subgui,'enable','off')

OpeningFcn

enter image description here

希望这有帮助。