Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
create a log box in matlab GUI

Subject: create a log box in matlab GUI

From: Mike

Date: 25 Jun, 2010 14:27:06

Message: 1 of 14

I am attempting to create a log box in the matlab gui but am having different issues with the different things I attempt.

What I am wanting to do is to have a box on my gui where I will display log messages in.

I tried to use the edit box to do this, but when I would try to enter another message to display it will clear my previous message. I want to be able to add another line onto this.

I tried to copy the current text to a string, then add another line to the string and display it that way but it didn't work.

I also tried to use uicontrol to make an edit box. Doing this I was able to get more than 1 line to be displayed, however when I go to another function in my matlab gui it overwrites the previous log messages.

What I want the log box to look like is something like the following:

If I push the 'Retrieve Data' button a message will display in the log box: 'Retrieving Data....'
when done retrieving it displays: 'Retrieving Data.... Done.'

Then I will press another button, 'Process Data' where it will display to log box: 'Processing Data....'

So I want the log box to look like:
-----
Retrieving Data.... Done.
Processing Data....
-----

I have looked all over the internet for someone else who has tried to do this but I haven't been able to find anything that works or that I can make sense out of.

Thank you for any help!

Subject: create a log box in matlab GUI

From: Andy

Date: 25 Jun, 2010 14:38:04

Message: 2 of 14

"Mike " <mikejcunningham@gmail.com> wrote in message <i02ebq$1qm$1@fred.mathworks.com>...
> I am attempting to create a log box in the matlab gui but am having different issues with the different things I attempt.
>
> What I am wanting to do is to have a box on my gui where I will display log messages in.
>
> I tried to use the edit box to do this, but when I would try to enter another message to display it will clear my previous message. I want to be able to add another line onto this.
>
> I tried to copy the current text to a string, then add another line to the string and display it that way but it didn't work.
>
> I also tried to use uicontrol to make an edit box. Doing this I was able to get more than 1 line to be displayed, however when I go to another function in my matlab gui it overwrites the previous log messages.
>
> What I want the log box to look like is something like the following:
>
> If I push the 'Retrieve Data' button a message will display in the log box: 'Retrieving Data....'
> when done retrieving it displays: 'Retrieving Data.... Done.'
>
> Then I will press another button, 'Process Data' where it will display to log box: 'Processing Data....'
>
> So I want the log box to look like:
> -----
> Retrieving Data.... Done.
> Processing Data....
> -----
>
> I have looked all over the internet for someone else who has tried to do this but I haven't been able to find anything that works or that I can make sense out of.
>
> Thank you for any help!

You should probably use a listbox for this. The messages themselves should be controlled by, for example, the pushbutton callback. (Every function that needs to report something to the log should take care of that on its own.)

Also, look on the file exchange for Matt Fig's GUI examples. They will show you how to do lots of basic things with GUIs.

Subject: create a log box in matlab GUI

From: Jan Simon

Date: 25 Jun, 2010 15:34:04

Message: 3 of 14

Dear Mike!

The task, you described, sounds definitely as a really fine job for the command window. Then some FPRINTF or DISP commands are easier to use than adding strings to the 'String' property of a listbox.

I've experimented with some methods to display logs also. But finally I decided to use a simple wrapper for DISP, which writes all messages to the command window and a file.

Jan

Subject: create a log box in matlab GUI

From: Frédéric Bergeron

Date: 25 Jun, 2010 15:59:04

Message: 4 of 14

"Mike " <mikejcunningham@gmail.com> wrote in message <i02ebq$1qm$1@fred.mathworks.com>...
> I am attempting to create a log box in the matlab gui but am having different issues with the different things I attempt.
>
> What I am wanting to do is to have a box on my gui where I will display log messages in.
>
> I tried to use the edit box to do this, but when I would try to enter another message to display it will clear my previous message. I want to be able to add another line onto this.
>
> I tried to copy the current text to a string, then add another line to the string and display it that way but it didn't work.
>
> I also tried to use uicontrol to make an edit box. Doing this I was able to get more than 1 line to be displayed, however when I go to another function in my matlab gui it overwrites the previous log messages.
>
> What I want the log box to look like is something like the following:
>
> If I push the 'Retrieve Data' button a message will display in the log box: 'Retrieving Data....'
> when done retrieving it displays: 'Retrieving Data.... Done.'
>
> Then I will press another button, 'Process Data' where it will display to log box: 'Processing Data....'
>
> So I want the log box to look like:
> -----
> Retrieving Data.... Done.
> Processing Data....
> -----
>
> I have looked all over the internet for someone else who has tried to do this but I haven't been able to find anything that works or that I can make sense out of.
>
> Thank you for any help!

Hey,

Have you tried writing the text for the 'string' property in a cell array, where different cells mean different lines?

Subject: create a log box in matlab GUI

From: Mike

Date: 25 Jun, 2010 16:18:19

Message: 5 of 14

Andy, I don't think the listbox will give me the format I want. But I will see what I can do with it.

Jan, that is what I've started to do recently, since I can't figure it out otherwise... I would rather not have to do that, but it's the 'if all else fails plan'.

Frédéric, I'm not sure if I know what you mean exactly, but I believe I have tried that with no avail.

Thanks for suggestions, will keep trying and post if I find a solution.

Subject: create a log box in matlab GUI

From: Jan Simon

Date: 25 Jun, 2010 16:28:21

Message: 6 of 14

Dear Mike,

Frédéric wrote:
> Have you tried writing the text for the 'string' property in a cell array, where different cells mean different lines?

E.g.:
H = uicontrol('Style', 'listbox', ...
   'Units', 'normalized', ...
   'Position', [0, 0, 1, 1], ...
   'String', {'First line'});
drawnow;
pause(1.0);

set(H, 'String', cat(1, get(H, 'String'), {'Second line'}));
drawnow;
pause(1.0);

set(H, 'String', cat(1, get(H, 'String'), {'Third line'}));
drawnow;
pause(1.0);

The DRAWNOW are important, if the PAUSE vanishes in a real program.
Of course you could collect the cell string anywhere else, e.g. as persistent variable, as UserData of the FIGURE, with SETAPPDATA. With the above method your program needs to remember only the handle of the listbox.

Nevertheless, I'd still prefer the standard command window.

Jan

Subject: create a log box in matlab GUI

From: Andy

Date: 25 Jun, 2010 17:27:04

Message: 7 of 14

"Mike " <mikejcunningham@gmail.com> wrote in message <i02ksb$c6s$1@fred.mathworks.com>...
> Andy, I don't think the listbox will give me the format I want. But I will see what I can do with it.
>

I don't know what you mean by "format". Here's an example:


function mygui
f=figure('units','pixels','position',[30 100 500 300]);
l=uicontrol('parent',f,'style','list','units','pixels','position',[30 10 300 250],'String',{'Starting'});
pb=uicontrol('parent',f,'style','push','units','pixels','position',[30 270 80 20],'string','Run','callback',{@pb_call});


function pb_call(varargin)
  cur_log = get(l,'String');
  new_log = [cur_log ; {'Running ...'}];
  set(l,'String',new_log);
  pause(1);
  new_log = [cur_log ; {'Running ... Done'}];
  set(l,'String',new_log);
end

end

Subject: create a log box in matlab GUI

From: Yair Altman

Date: 26 Jun, 2010 17:49:05

Message: 8 of 14

> Andy, I don't think the listbox will give me the format I want. But I will see what I can do with it.

You can actually do a lot by customizing a multi-line editbox, as shown here: http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/ (scroll down until you see the log example and screenshot). All it takes is a little bit of Java magic powder...

For the record, you can also achieve similar results with a listbox

Yair Altman
http://UndocumentedMatlab.com

Subject: create a log box in matlab GUI

From: Eddie

Date: 31 Aug, 2010 17:30:24

Message: 9 of 14

Hi

Is there a way to make the list box display the newest line after it fills? (i.e. I want to view the latest log message and not have to move the scroll bar).

Cheers

H = uicontrol('Style', 'listbox', ...
   'Units', 'normalized', ...
   'Position', [0, 0, 1, 1], ...
   'String', {'First line'});
drawnow;
pause(1.0);

for ii=1:40
   set(H, 'String', cat(1, get(H, 'String'), {['line ' num2str(ii)]}));
   drawnow;
   pause(0.25);
end

set(H, 'String', cat(1, get(H, 'String'), {'Last line'}));
drawnow;
pause(1.0);

Subject: create a log box in matlab GUI

From: Yair Altman

Date: 31 Aug, 2010 21:59:07

Message: 10 of 14

"Eddie " <epatton@yahoo.com> wrote in message <i5je7f$rda$1@fred.mathworks.com>...
> Hi
>
> Is there a way to make the list box display the newest line after it fills? (i.e. I want to view the latest log message and not have to move the scroll bar).


Yes - see the following related articles:
http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/
http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/
http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/#EditorKit

Yair Altman
http://UndocumentedMatlab.com

Subject: create a log box in matlab GUI

From: Eddie

Date: 31 Aug, 2010 23:02:06

Message: 11 of 14

Hi

Is there a way to make the list box display the newest line after it fills? (i.e. I want to view the latest log message and not have to move the scroll bar).

Cheers

H = uicontrol('Style', 'listbox', ...
   'Units', 'normalized', ...
   'Position', [0, 0, 1, 1], ...
   'String', {'First line'});
drawnow;
pause(1.0);

for ii=1:40
   set(H, 'String', cat(1, get(H, 'String'), {['line ' num2str(ii)]}));
   drawnow;
   pause(0.25);
end

set(H, 'String', cat(1, get(H, 'String'), {'Last line'}));
drawnow;
pause(1.0);

Subject: create a log box in matlab GUI

From: Eddie

Date: 1 Sep, 2010 23:31:20

Message: 12 of 14

Hi

Matlab 2008b

I tried the example and ran into the error

    ??? No appropriate method, property, or field getDocument for class
    com.mathworks.hg.peer.ListboxPeer$UicontrolList.

when I tried to set the carrot. The output is below (wish I could color or bold the commands to make it easier for you).

It appears that when I issued the .list, there was no multiline control, yet it appears I have the right handle when I compare the output in your link

http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/

Cheers

*****************output from MATLAB ********************

K >> jhEdit = findjobj(handles.listbox1)
jhEdit =
javahandle_withcallbacks.com.mathworks.hg.peer.utils.UIScrollPane

K>> jhEdit.list
com.mathworks.hg.peer.utils.UIScrollPane[,0,0,170x132,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.basic.BasicBorders$FieldBorder@352ec,flags=16777544,maximumSize=,minimumSize=,preferredSize=,columnHeader=,horizontalScrollBar=com.mathworks.hg.peer.utils.UIScrollPane$2[,0,0,0x0,hidden,layout=com.sun.java.swing.plaf.windows.WindowsScrollBarUI,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,blockIncrement=10,orientation=HORIZONTAL,unitIncrement=1],horizontalScrollBarPolicy=HORIZONTAL_SCROLLBAR_AS_NEEDED,lowerLeft=,lowerRight=,rowHeader=,upperLeft=,upperRight=,verticalScrollBar=com.mathworks.hg.peer.utils.UIScrollPane$1[,152,2,16x128,layout=com.sun.java.swing.plaf.windows.WindowsScrollBarUI,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,blockIncre
ment=10,orientation=VERTICAL,unitIncrement=1],verticalScrollBarPolicy=VERTICAL_SCROLLBAR_ALWAYS,viewport=javax.swing.JViewport[,2,2,150x128,layout=javax.swing.ViewportLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,isViewSizeSet=true,lastPaintPosition=java.awt.Point[x=0,y=0],scrollUnderway=false],viewportBorder=]
 javax.swing.JViewport[,2,2,150x128,layout=javax.swing.ViewportLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,isViewSizeSet=true,lastPaintPosition=java.awt.Point[x=0,y=0],scrollUnderway=false]
  com.mathworks.hg.peer.ListboxPeer$UicontrolList[,0,0,150x128,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=13,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=212,g=208,b=200],selectionForeground=java.awt.Color[r=0,g=0,b=0],visibleRowCount=8,layoutOrientation=0]
   javax.swing.CellRendererPane[,0,0,0x0,hidden]
 com.mathworks.hg.peer.utils.UIScrollPane$1[,152,2,16x128,layout=com.sun.java.swing.plaf.windows.WindowsScrollBarUI,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,blockIncrement=10,orientation=VERTICAL,unitIncrement=1]
  com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[,0,112,16x16,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@391879,flags=4194600,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]
  com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[,0,0,16x16,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@75ea85,flags=4194600,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]
 com.mathworks.hg.peer.utils.UIScrollPane$2[,0,0,0x0,hidden,layout=com.sun.java.swing.plaf.windows.WindowsScrollBarUI,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,blockIncrement=10,orientation=HORIZONTAL,unitIncrement=1]
  com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[,0,0,0x0,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@177c306,flags=4194600,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]
  com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[,0,0,0x0,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1a4c4cb,flags=4194600,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

K>> jEdit = jhEdit.getComponent(0).getComponent(0)
 
jEdit =
 
com.mathworks.hg.peer.ListboxPeer$UicontrolList[,0,0,150x128,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=13,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=212,g=208,b=200],selectionForeground=java.awt.Color[r=0,g=0,b=0],visibleRowCount=8,layoutOrientation=0]


K>> jEdit.setCaretPosition(jEdit.getDocument.getLength)
??? No appropriate method, property, or field getDocument for class
com.mathworks.hg.peer.ListboxPeer$UicontrolList.

Subject: create a log box in matlab GUI

From: Yair Altman

Date: 11 Sep, 2010 22:52:05

Message: 13 of 14

"Eddie " <epatton@yahoo.com> wrote in message <i5mno8$t94$1@fred.mathworks.com>...
> Hi
>
> Matlab 2008b
>
> I tried the example and ran into the error
>
> ??? No appropriate method, property, or field getDocument for class
> com.mathworks.hg.peer.ListboxPeer$UicontrolList.
>
> when I tried to set the carrot. The output is below (wish I could color or bold the commands to make it easier for you).

This is because you use a listbox, not an editbox - the code you used was for an editbox. They are similar but not exactly the same. In fact, for a listbox you do not need to use Java or any other undocumented feature: simply set the listbox's ListboxTop property value to the number of listbox items:

set(handles.listbox1, 'ListboxTop',length(get(handles.listbox1,'String')));

Unfortunately, this simple solution is unavailable in editboxes - in these cases you need to use the undocumented Java hacks mentioned above.

Yair Altman
http://UndocumentedMatlab.com

Subject: create a log box in matlab GUI

From: Eddie

Date: 15 Sep, 2010 18:34:05

Message: 14 of 14

Hi Yair

Thanks for the pointer. I also see that others have used this approach:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285720

I also found that I had to use size() instead of length because on the first pass, length would return a value of zero and give me the error below.

Thanks again for your help.

Cheers

********** Error when using length parameter **********************
127 set(handles.listbox1, 'ListboxTop',length(get(handles.listbox1,'String')));
Warning: This uicontrol's ListboxTop value (10) must be within the
String range (1:1)
> In testListBox_eddie>pbAddOneLine_Callback at 132
  In gui_mainfcn at 96
  In testListBox_eddie at 42

***********snippet of working code******************************
oldLines = get(handles.listbox1, 'String');
newLine = ['new line ' num2str(a)];
if(isempty(oldLines))
   newLines = newLine;
else
   newLines = strvcat(oldLines, newLine);
end
set(handles.listbox1, 'String', newLines);
numLines = size(get(handles.listbox1,'String'));
numLines = numLines(1);
display(['Num lines = ' num2str(numLines)])
set(handles.listbox1, 'ListboxTop', numLines);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us