Matt Griffin
Matt Griffin

Reputation: 95

Add items to listbox using pushbutton and drop down menu in MATLAB

I am creating a GUI using GUIDE in MATLAB2015. I have a drop down menu which the user selects a message to view, after that they click the Add push button to add the message name to a list box and display the data it contains in a table.

My problem is, if I want to add more than one message, instead of adding that message, it overwrites the previous one. Below is my current code.

addData = getappdata(handles.msgSel_menu, 'Data');
boxMsg = get(handles.msgSel_menu,'String');
boxMsgVal = get(handles.msgSel_menu,'Value');
set(handles.activeDataBox,'String',boxMsg{boxMsgVal});
set(handles.data_table, 'Data', addData);

Apologies if this has been done multiple times before, but as I'm relatively new to MATLAB I could do with a little explanation of any code that fixes my issue.

Upvotes: 0

Views: 434

Answers (1)

oro777
oro777

Reputation: 1110

You have to get the initial string cell from your listbox then you add the desired element to the cell. From the resulting cell variable, you update the listbox using the set command.

Here is a simple example.

addData = getappdata(handles.msgSel_menu, 'Data');
current_data = get(handles.activeDataBox, 'String'); % get the current string
new_data = current_data; % set new_data to the initial string ...
new_data{ length(current_data) + 1 } = addData ; % ... then you add the desired element by incrementing the cell
set(handles.activeDataBox, 'String', new_data); % update your listbox

EDIT : The code has been updated.

Upvotes: 1

Related Questions