Reputation: 391
I'm building a GUI using GUIDE and I have a listbox where I want to receive several messages after I click in send_button
but every time I click on the button the message shows in the first line.
function send_button_Callback(hObject, eventdata, handles)
% hObject handle to send_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get text
dstID = get(handles.dest_ID,'String');
msg = get(handles.message_box,'String'); % message_box = editbox
% Build message and send
msg = {['< ', dstID, ' > ', msg]}; % dstID = number
set(handles.message_list, 'String', msg); % message_list = listbox
What should I do in order to have something like
<3> Message one
<3> Message two
<3> Message three
I think this happens because msg
is a String but I don't know how to insert a '\n'
or something like that.
Upvotes: 1
Views: 8413
Reputation: 1629
If you want to append new messages to the top, you need a cell array that starts with your new message. If your messages are too long, reshape them. Note that in the following example, the first line might be too long if dstID
is greater than 9, you might want to correct maxlinelength
to account for that.
maxlinelength = 25; %// maximum number of characters per line -5
current = get(handles.message_list,'string');
msg = get(handles.message_box,'String'); %// message_box = editbox
rows = ceil(length(msg)/maxlinelength);
msg = [char(8,8,8,8)' '< ' dstID ' > ' msg]; %'// append dstID display
newmsg = cell(1,rows);
for i=1:rows-1 %// for each row
newmsg{i} = [' ' a(1:maxlinelength)]; %// store the row in the new cell
a = a(maxlinelength:end); %// cut off the row from the message
end
newmsg{rows} = [' ' a]; %// last cell is the rest of msg
new = {msg,current{:}}; %// build new cell aray with msg at the front
set(handles.message_list, 'String', new); %// message_list = listbox
If you want to display a maximum of lines, you can also cut off new
before setting it as 'String'
:
if length(new)>maxlines
new = new(1:maxlines);
end
The line with char(8,8,8,8)'
puts backspaces in front of msg
to delete the 4 spaces that will be added later. If you change your newline indentation, change the number of 8's here as well. For example, if you choose to indent by 2 spaces, this becomes char(8,8)'
.
Upvotes: 0
Reputation: 1832
Try this:
...
msg=get(handles.message_box,'String'); % message_box = editbox
...
cell_listbox = get(handles.message_list,'string');
...
cell_listbox(end+1)=msg;
and if you get an error, please provide in which line it comes up :)
Upvotes: 0
Reputation: 193
You can get the cell array of strings containing items of your listbox with get(handles.message_list,'string')
. Here's a way to solve your problem.
function send_button_Callback(hObject, eventdata, handles)
% hObject handle to send_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get text
dstID = get(handles.dest_ID,'String');
msg = get(handles.message_box,'String'); % message_box = editbox
%get lisbox cell array of strings
cell_listbox = get(handles.message_list,'string');
%length is needed in order to append the desired message at the end
length_cell_listbox = length(cell_listbox);
% Build message and send
msg = ['< ', dstID, ' > ', msg]; % dstID = number
cell_listbox{length_cell_listbox + 1} = msg;
set(handles.message_list, 'String', cell_listbox); % message_list = listbox
Using the same idea you can even create a button that deletes the last message stored in your listbox.
Upvotes: 2