Open In App

Label component in MATLAB GUI

Matlab is a numeric computing environment that comes with its own programming language. It specializes in technical computing in areas like Science, Engineering, and many others. Matlab also provides the ability to create GUI applications by simply using its functions for various GUI components.

In this article, we will learn about the Label component, how to create one, and its important properties.



 Label Component 

A label is a UI component that holds the static text to label different parts of an application. It is useful in GUI applications as it describes the different parts of the UI. Matlab provides a function called uilabel to create a label. There are three syntaxes that can be used:

Properties of Uilabel component

To control the appearances and behaviors of the component, Matlab provides many properties. Some important properties are as follows:



 Uilabel()

The first syntax does not need any parameters. It is created with “Label” as the default text. Matlab creates a figure window and stores the label into the window for us.

Example:




% MATLAB code for Uilabel() demonstration.
% create a uilabel using only the function
label = uilabel;

Output:

uilabel(parent)

Matlab also provides the option to pass a custom window as the parent of the component.

Example:




% MATLAB code for Uilabel(parent) demonstration.
% create a figure
fig = uifigure;
  
% create a label and pass the figure as parent
label = uilabel(fig);

Output:

uilabel(parent, Name, Value)

Matlab also provides us with the option to pass the value to the component using name-value pairs.

Example:




%  MATLAB code for uilabel(parent, Name, Value) function
% create a figure
fig = uifigure;
  
% create a label and pass the figure as parent
label = uilabel(fig, 'Text', 'Enter Fruits Names:');

Output:

We can see that the size of the component is small and hence the text is clipped. We can fix the issue by modifying the size of the label component.

Example:




% create a figure
fig = uifigure;
  
% create a label and pass the figure as parent
label = uilabel(fig, 'Text', 'Enter Fruits Names:');
  
% changing the size of the  
label.Position(3:4) = [120, 22];

Output:

The position property is a 4 valued list where the first 2 values are the positions and the last 2 values are the sizes of the component. In the code above we have changed the value of the last two indices (3:4).


Article Tags :