Download this file

47 lines (39 with data), 1.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function callbackexample()
% Copyright 2009-2013 The MathWorks, Inc.
% Create application data
colorNames = {
'Red'
'Orange'
'Yellow'
'Green'
'Blue'
'Indigo'
'Violet'
};
colorValues = [
1.0 0.2 0.2
1.0 0.6 0.2
1.0 1.0 0.4
0.6 1.0 0.6
0.2 0.4 1.0
0.4 0.1 0.6
0.7 0.5 1.0
];
% Layout the interface
f = figure();
p = uix.Panel( 'Parent', f, 'Title', 'A Panel', 'TitlePosition', 'CenterTop');
b = uix.HBoxFlex( 'Parent', p, 'Spacing', 5, 'Padding', 5 );
hList = uicontrol( 'Style', 'listbox', 'Parent', b, ...
'String', colorNames, ...
'Back', 'w' );
hButton = uicontrol( 'Parent', b, ...
'Background', colorValues(1,:), ...
'String', colorNames{1} );
set( b, 'Widths', [-1 -3] );
% Add user interactions
set( hList, 'Callback', @onChangeColor );
function onChangeColor( source, ~ )
idx = get( source, 'Value' );
set( hButton, 'Background', colorValues(idx,:), 'String', colorNames{idx} )
end % onChangeColor
end % main