Andry
Andry

Reputation: 16845

Handling carriage return/line feed in MATLAB GUI user controls

I have a MATLAB program I am developing in order to do some image processing stuff and need to use a user control into a MATLAB GUI user interface I created ad-hoc.

This user control is a List Box and there I would like to insert some text. Well the problem is not that I cannot put text there, I can do that using this call:

set(handles.mylistbox, 'String', 'MyStringToPrint');

Well the problem is that this call does not let me insert many lines in my list box but just overwrite the previous one.

I wish to find a way to let my code insert the new text in a new line. This should not be so difficult to do and it is also a simple pattern:

texttoprint = 'My text to add'
oldtext = get(handles.MyListBox, 'String') %Holding the previous text here
set(handles.MyListBox, 'String', [oldtext '\n' texttoprint]) %Setting (no line feed printed)
set(handles.MyListBox, 'String', [oldtext char(10) texttoprint]) %Setting (this fails too)

Well it is ok and it does not raise any error BUT, \n DOES NOT WORK. I do not have any new line... BUT NEED TO!!!!

How should I solve this? The problem is that I need to print text in this user control, not on the MATLAB commandline (that is very simple just by doing sprintf()).

What to do? Thank you

Upvotes: 1

Views: 9395

Answers (2)

Jonas
Jonas

Reputation: 74940

For a listbox, set the string property to a cell

set(myListboxHandle,'String',{'myFirstLine';'mySecondLine'})

If you want to add another line, call

contents = get(myListboxHandle,'String');
set(myListboxHandle,[contents;{'another line'}])

For multiline text in GUIs otherwise, use char(10) instead of \n, i.e.

set(someUiControlHandle,'String',sprintf('my first line%smy second line',char(10)))

Upvotes: 4

gnovice
gnovice

Reputation: 125854

When working with list boxes it's usually easier to deal with the options as a cell array of strings. So, you would initialize your list box as follows:

set(handles.MyListBox,'String',{'Option 1'});

And then you can add options to your list box like so:

newOption = 'Option 2';
oldOptions = get(handles.MyListBox,'String');
set(handles.MyListBox,'String',[oldOptions; {newOption}]);

Upvotes: 0

Related Questions