Programmatic Component Creation
While the Component Library supports drag-and-drop placement for most UI elements, specific development scenarios require programmatic instantiation. This approach is essential when creating custom interfaces not found in the standard library or when generating elements dynamically based on runtime conditions. To implement programmatic UI elements, the developer must invoke the specific component constructor, link it to a callback function, and define the logic for that callback.
Instantiating Objects and Linking Events
Component instantiation usually occurs within an existing callback routine. The StartupFcn is frequently used for this purpose, as it executes immediately upon the application launch. Alternatively, components can be generated in response to specific user interactions, such as displaying a dialog when a button is pressed. When calling a creation function, the parent container—either the main figure or a child panel—must be explicitly specified.
btn = uibutton(app.MainFigure);Once the component is created, assign its callback property to a function handle referencing the app's method. For instance, the following command binds the button's ButtonPushedFcn to a method named onButtonPress:
btn.ButtonPushedFcn = @app.onButtonPress;Defining Callback Methods
Callbacks should be implemented as private helper methods within the app class. These methods must accept three specific arguments in the following order: the app instance, the source component, and the event data. Below is an example of such a method implementation:
methods (Access = private)
function onButtonPress(app, source, eventData)
disp('Operation triggered successfully.');
end
endPassing Additional Arguments
To pass supplementary parameters to the callback function, append them to the argument list after the standard three inputs. The following example defines a callback that accepts two extra numerical arguments, x and y:
methods (Access = private)
function calculateSum(app, source, eventData, x, y)
disp(x + y);
end
endWhen assigning this callback to a component property, wrap the function handle and the additional arguments within a cell array. The first element is the handle, followed by the data values:
btn.ButtonPushedFcn = {@app.calculateSum, 50, 25};Use Case: Confirming Application Closure
This implementation demonstrates how to utilize a confirmation dialog that executes logic upon closing. When the user attempts to terminate the application by clicking the window's close button, a dialog appears requesting confirmation. If the user confirms the action, the CloseFcn callback is triggered to perform final operations.
Use Case: Dynamic Tree Population
This scenario illustrates dynamically populating a uitree component based on external data. Initially, the tree contains only root-level nodes representing different facilities. During runtime, the application reads a data file (e.g., inventory.csv) to generate child nodes under each facility. The specific count and labels of these child nodes are determined by the file's content. Selecting a specific item within the tree updates a details panel with relevant attributes such as ID, status, and category, while the application persists any modifications in a table array.