Open In App

How to create a textarea component in MATLAB

Matlab offers tools for developing GUI applications. It provides functions that can create TextFields, Labels, Buttons, and many more, along with properties to manipulate the components. In this article, we will learn to create a TextArea Component using Matlab.

Creating a textarea component

A Text Area component is a UI component that allows us to input multiple lines of text. To create a text area object, we use the Matlab function uitextarea(), which accepts two optional parameters, the parent window in which this component resides and its values.



uitextarea(parent, Name:Value);

The parent is a figure or window that will hold all the components of our GUI. The Name-Value pair is the value that the text area will be initialized with.



Properties of textarea component

Properties of UI components are used to access and mutate their content as well as their appearances. Use dot notation to refer to a specific property.

A few of the important properties of the text area component are as follows:

There are three syntaxes of uitextarea:

Now we see the first syntax without any parameters. It creates the text area component and the Matlab creates a figure window to hold the component.

Example 1: 




% MATLAB code for
% creating a textarea
textareaObject = uitextarea;

Output:

uitextarea also accepts an optional parent container or window. If you have defined a window and want to add a text area to it, then textareaObject = uitextarea(parent) used.

Example 2: 




% MATLAB code for create a textarea
% and pass the figure as parent
% create a figure
fig = uifigure;
textareaObject = uitextarea(fig);

Output:

We also have the option to set the properties while instantiating a text area.

Example 3: 




% create a figure window
fig = uifigure;
 
% create a text area with figure as parent
txa = uitextarea(fig, 'Value', {'Mango';'Apple';'Litchi'});
 
% set the position and size of the text area
txa.Position = [100,100,150,150];

Output:

 

Using uifigure, we created a figure window which will be our parent window. It will hold other components. The uitextarea() is used to create a text area component, and we pass the parent window as its parameter and a key-value pair to set its property Value, which determines the text area’s content. The Position property accepts a 4 value list, the first 2 values are the component’s position in the parent window, and the last two are the size of the components (width and height).

Example 4:




% MATLAB code for text area with Scrollbar 
fig = uifigure;
textareaObject = uitextarea(fig);
 
% setting the position of the
% text area within the window.
textareaObject.Position = [100 100 80 80];
textareaObject.Value = 'Apple Mango Banana Litchi guava Pineapple Watermelon Orange grapes Pomegranate';

Output before scroll:

Now, we can use the scroll function with the component and the location where to scroll like ‘bottom’. as parameter.

Syntax: 

scroll(textareaObject, ‘bottom’);

Output after scroll:


Article Tags :