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