Advanced Matlab GUI
Open Day Jan 8, 2013
[Link]
Yair Altman [Link] 1
GUIDE: Graphical User Interface Design Env
Yair Altman
[Link]
GUIDE: Creating a simple GUI
>> guide
drag & drop
Double click to customize
Yair Altman
[Link]
Available uicontrols
Push-button Slider (scrollbar) Radio button Checkbox Editbox Text label Popup menu (combobox, drop-down) Listbox
Yair Altman
Toggle button Uitable (R2008a+) Plot axes Panel Button group ActiveX control
[Link]
Advanced GUI topics
The GUIDE-generated file duo OpeningFcn vs. OutputFcn Setting and using callbacks Callback event handling, reentrancy, dynamic data The hObject parameter The eventdata parameter The handles struct guihandles vs. guidata Controlling handles visibility
Yair Altman [Link] 5
Using HTML
All Matlab GUI is based on Java Swing
o Java Swing supports HTML labels + extensive CSS subset
HTML processing is CPU intensive bad for performance
o Especially bad if recurring multiple times (table/listbox cells)
Use only if <20-30 elements
o Alternatives: built-in Font* properties; Java cell renderers
Case-insensitive HTML tags, attributes
Required: <html> prefix at the string's beginning
Not required: closing tags (</b></font></html>)
Note: HTML processing may modify some display aspects
o Font, background color, label margins (visible in tooltips)
Yair Altman [Link] 6
Using HTML some examples
Listbox
uicontrol('Style','list', 'Position',[10,10,70,70], 'String', ... {'<HTML><FONT color="red">Hello</Font></html>', 'world', ... '<html><font style="font-family:impact;color:green"><i>What a', ... '<Html><FONT color="blue" face="Comic Sans MS">nice day!</font>'});
Combo-box
uicontrol('Style','popup', 'Position',[10,10,150,100], 'String', ... {'<HTML><BODY bgcolor="green">green background</BODY></HTML>', ... '<HTML><FONT color="red" size="+2">Large red font</FONT></HTML>', ... '<HTML><BODY bgcolor="#FF00FF"><PRE>fixed-width font'});
Yair Altman
[Link]
Using HTML some examples
Fonts in combo-box
fontStr = @(font) ['<html><font face="' font '">' font '</font></html>']; htmlStr = cellfun(fontStr,listfonts,'uniform',false); uicontrol('style','popupmenu', 'string',htmlStr, 'pos',[20,350,60,20]);
Yair Altman
[Link]
Using HTML images
HTML img src must have full pathname in URI format:
>> uicontrol('Position',..., 'String','<Html><img src="[Link]">'); >> uicontrol('Style','list', ... '<Html><img src="[Link]"/>'});
>> iconsFolder = fullfile(matlabroot,'/toolbox/matlab/icons/'); >> iconUrl = strrep(['file:/' iconsFolder '[Link]'],'\','/'); >> str = ['<Html><img src="' iconUrl '">']
str = <Html><img src="file:/C:/Program Files/MATLAB/.../icons/[Link]">
>> uicontrol('Position',..., 'String',str); >> uicontrol('Style','list', ... str});
Yair Altman
[Link]
Using HTML images
HTML images can also be placed in tooltips:
filePath = 'C:\Yair\Undocumented Matlab\Images\[Link]'; str = ['<html><center><img src="' filePath '"><br>' filePath]; set(hButton,'tooltipString',str);
filePath = 'C:\Yair\Undocumented Matlab\Images\[Link]'; filePath = strrep(['file:/' filePath],'\','/'); str = ['<html><center><img src="' filePath '"><br>' ... '<b><font color="blue">' filePath]; set(hButton,'tooltipString',str);
Yair Altman
[Link]
10
Hidden properties
Most Matlab graphics components (GUI, plots, ) have hidden unsupported/undocumented properties These properties behave exactly like regular properties, except that they are not displayed by default in get/set To display these properties:
o set(0,'HideUndocumented','off'); % default='on' o HideUndocumented is itself a hidden property o Or use the FEX: getundoc utility (32934)
Some hidden properties have no effect, some are useful
o scatter-plot Jitter, plot LineSmoothing, axes LooseInset, figure JavaFrame
Hidden properties are unsupported and may be removed in any future Matlab release without prior notice
Yair Altman [Link] 11
Hidden property example LineSmoothing
[Link]/blog/plot-linesmoothing-property/
[Link]/matlabcentral/fileexchange/20979-Myaa
Yair Altman
[Link]
12
Displaying data in a uitable
>> >> >> >> data = rand(3); headers = {'X-Data', 'Y-Data', 'Z-Data'}; rowNames = {'First', 'Second', 'Third'}; hTable = uitable('Parent',gcf, ... 'Data',data, ... 'Pos',[20,0,360,90], ... 'ColumnName',headers, ... 'RowName',rowNames);
Yair Altman
[Link]
13
Customizing uitable appearance
>> set(h,'ColumnWidth',{25}) >> set(h,'ColumnWidth',{45,60,35,80})
>> set(h,'BackgroundColor',[.4,.7,.3]) >> set(h,'ForegroundColor',[.2,.3,.8])
>> set(h,'RowStriping','off') >> set(h,'RearrangeableColumns','on')
Yair Altman
[Link]
14
HTML support
>> data{2,2} = '<html><b><i><font color="red" size=+1>big red' data = [6.125] [ 456.3457] [1] [ 6.75] '<html><b><i><font color="red" size=+1>big red' [0] [ 7] [ 658.2] [0] >> set(h,'Data',data)
'Fixed' 'Adjustable' 'Fixed'
>> tooltipStr = '<html><b>line #1</b><br><font color="red"><i>line #2'; >> set(h,'TooltipString',tooltipStr)
Yair Altman
[Link]
15
Advanced customizations using Java
Sorting and filtering
Customized cell renderer
Customized cell editor
Yair Altman
[Link]
16
Java components in Matlab figure
Yair Altman
[Link]
17
Java components in Matlab figure
FEX: SpinnerDemo (#26970)
Yair Altman [Link] 18
Feedback for long-duration tasks
FEX: Statusbar (#14773):
An indeterminate progress bar (JProgressBar):
Yair Altman [Link] 19
Matlab's slider uicontrol
Continuous dragging callback possible via [Link]
hListener = [Link](hSlider,'ActionEvent',@myCbFcn);
The standard Matlab slider uicontrol is actually a scrollbar with Windows-95 look-and-feel:
jScrollbar = javaObjectEDT([Link]); [Link]([Link]); javacomponent(jScrollbar,[10,40,200,20]); uicontrol('style','slider', 'position',[10,10,200,20]);
JScrollBar uicontrol('slider')
An actual Java slider:
jSlider = javaObjectEDT([Link]); set(jSlider, 'Value',22, 'MajorTickSpacing',20, 'PaintTicks',true); [Link](true); % or: [Link](1);
Yair Altman
[Link]
20
Example: mwswing CheckBoxTree
import [Link].* jRoot = DefaultCheckBoxNode('Root'); l1a = DefaultCheckBoxNode('Letters'); [Link](l1a); l1b = DefaultCheckBoxNode('Numbers'); [Link](l1b); l2a = DefaultCheckBoxNode('A'); [Link](l2a); l2b = DefaultCheckBoxNode('b'); [Link](l2b); l2c = DefaultCheckBoxNode('<html><b>α'); [Link](l2c); l2d = DefaultCheckBoxNode('<html><i>β'); [Link](l2d); l2e = DefaultCheckBoxNode('3.1415'); [Link](l2e); % Present the standard MJTree: jTree = [Link](jRoot); jScrollPane = [Link](jTree); [jComp,hc]=javacomponent(jScrollPane,[10,10,120,110],gcf); % Now present the CheckBoxTree: jCheckBoxTree = CheckBoxTree([Link]); jScrollPane = [Link](jCheckBoxTree); [jComp,hc]=javacomponent(jScrollPane,[150,10,120,110],gcf);
Yair Altman
[Link]
21
Color-selection combo-boxes
[Link]:
[Link]:
[Link] (R2010a-):
Yair Altman
[Link]
22
JIDE date-chooser components
DateChooserPanel
MonthChooserPanel CalendarViewer
DateComboBox
Yair Altman
DateSpinnerComboBox
[Link]
MonthComboBox
23
Sample Java integrated in Matlab GUI
Yair Altman
[Link]
24
FEX: findjobj (#14317)
Yair Altman
[Link]
25
Integrating 3rd-party libs: JFreeChart
Yair Altman
[Link]
26
Integrating 3rd-party libs: ImageJ
Yair Altman
[Link]
27
Conclusion
Standard Matlab GUI is easy to set-up but limited However, extremely easy to customize & extend Multiple undocumented properties, functions enable nice customizations, even without Java Entire power of Java is also directly available, and can be combined with regular Matlab GUI Matlab GUI is limited by our design imagination more than by actual technical limitations
Yair Altman [Link] 28