I'm trying to build a GUI main routine that calls external functions to perform updates such as create_ui_buttons.m (to create my user interface buttons) and create_callback_fns.m (to create my callback functions). That is, outside of main, I want to define my callback functions and buttons in separate files (because the main file is getting long as is) but have main call them.
For example, I'd like to define my go button:
handles.go_button = uicontrol('style','pushbutton', 'Units', 'normalized', ...
'Position',[.1, .1, .1, .1], 'BackgroundColor', [0,1,0], ...
'FontWeight','bold', 'String', 'Go!', 'callback',@go_button_callback);
in a file called create_ui_buttons.m and have it know to look in another file create_callback_fns.m for the @go_button_callback reference. Right now, it is not seeing that reference.
As perhaps a more clear example, consider the uicontrol documentation from matlab:
function myui
% Create a figure and axes
f = figure('Visible','off');
ax = axes('Units','pixels');
surf(peaks)
% Create pop-up menu
popup = uicontrol('Style', 'popup',...
'String', {'parula','jet','hsv','hot','cool','gray'},...
'Position', [20 340 100 50],...
'Callback', @setmap);
% Create push button
btn = uicontrol('Style', 'pushbutton', 'String', 'Clear',...
'Position', [20 20 50 20],...
'Callback', 'cla');
% Create slider
sld = uicontrol('Style', 'slider',...
'Min',1,'Max',50,'Value',41,...
'Position', [400 20 120 20],...
'Callback', @surfzlim);
% Add a text uicontrol to label the slider.
txt = uicontrol('Style','text',...
'Position',[400 45 120 20],...
'String','Vertical Exaggeration');
% Make figure visble after adding all components
f.Visible = 'on';
% This code uses dot notation to set properties.
% Dot notation runs in R2014b and later.
% For R2014a and earlier: set(f,'Visible','on');
function setmap(source,event)
val = source.Value;
maps = source.String;
% For R2014a and earlier:
% val = get(source,'Value');
% maps = get(source,'String');
newmap = maps{val};
colormap(newmap);
end
function surfzlim(source,event)
val = 51 - source.Value;
% For R2014a and earlier:
% val = 51 - get(source,'Value');
zlim(ax,[-val val]);
end
end
How can I define the functions setmap and surfzlim in another function called create_callback_fns.m and how can I define popup and sld in another function called create_ui_buttons.m outside of main and have them all communicate?
*.mfile for every function or look into using an object oriented approach for your GUI. Using an abstract class with static methods that you either inherit or call directly would be a straightforward approach for your callbacks.*.mfile for every function? I've createdsetmap.mandsurfzlim.m, but how do I call those / reference those with function handles / callbacks? I was stuck using@setmap...setmap.mthen your Callback is defined as@setmap.setmapthat it takessourceandeventinput? What are these inputs and how do I pass them from the main GUI to the callback?