Matlab

# The isempty Function in MATLAB

When we call functions and send empty scalar vectors or matrices, the results can be wrong or create an exception in the system.

In this article, you will learn everything you need to know to implement the isempty() function in MATLAB that determines if a vector, scalar, matrix, or object is empty.

Next, we will look at a complete description of the syntax and structure of this function. We will also detail the input and output arguments and the data types each accepts.

Following the description, we will implement this function in practical examples that we have prepared for you with code snippets and images showing the different ways isempty() can be used in MATLAB.

## MATLAB isempty Function Syntax

TF = isempty ( a )

### MATLAB isempty Function Description and Examples

The isempty() function belongs to MATLAB’s “is…” family. This set of functions is used to determine whether an array “is” of a particular data type. They are often implemented as a condition in if-conditions, as they come in handy when we create functions to determine whether or not the input argument sent in the function call is compatible with the function. Thus, it avoids errors or exceptions due to data incompatibility.

The MATLAB function, isempty(), determines whether the input array “A” is empty. It returns a logical value equal to 1 at the output “TF” if it is empty or a logical 0 if the array is empty. The input argument “A” accepts scalars, vectors, 2D arrays, multidimensional arrays, tables, and time tables.

This function is also beneficial in determining the state of inputs and outputs in dynamic system models. In this case, the model, “sys”, is sent as an input argument, and isempty() returns a logical 1 if “sys” has any empty dimensions. Also, this function returns a logical 1 if the frequency vector of the model is empty.

If the input argument to this function is not a scalar, isempty() returns “true” or a logical 1 only if all elements of the input vector, array, or table are empty. If only one of the input array elements ever contains a value, the result of the function is a logical 0 or false.

## How To Determine If a Scalar is Empty With MATLAB’s isempty() Function

In this example, we’ll use the isempty() function to determine if a scalar is empty. To simplify the example, we specify the value in the input argument. The following code snippet calls the isempty() function three times with different inputs. The first call sends a scalar with an integer value and the second an empty character, as shown in the following image:

isempty ( 5 )

isempty ( '' )

In the following figure, we can see the result for each. In the first case, the scalar had an integer value, so isempty() returned 0 in TF. The scalar was sent empty in the following case, so the function returned true or =1. ## How To Determine If a Vector is Empty With MATLAB isempty() Function

When we call this function sending a vector as input arguments, isempty() will return true or =1 only when all the elements making up the vector are empty. Isempty() will consider a vector non-empty when one or more elements contain some type of data.

The following code snippet shows three calls to the isempty() function: the first when the vector “a” is empty, the second when the vector “b” consists of empty characters, and the third with the vector “c” having only one value in the first element and the rest being empty.

a = [];

isempty ( a )

b = [ '' , '' , '' , '' ];

isempty ( b )

c = [ '' , '' , '' , 'M' ];

isempty ( c )

The following image shows the results for the previous three cases. As we can see, in the case of the vector “c”, where only one element is non-empty, isempty() returns a logical 0. ## How To Determine If an Array is Empty With MATLAB isempty() Function

In this example, we will use the isempty() function to determine if the array “a” is empty. To do this, we create the array “a” and send it as an input argument to isempty. The method of the function call is the same as the one we used in the previous example.

a = [ 1, 2, 3; 3, 2, 1; 1, 2, 3];

isempty ( a )

b = [;;;];

isempty ( b )

As seen in the following figure, isempty() returns a logical 0 in the matrix “a” since it is not empty. It returns a logical 1 in the case of matrix “b”, which is empty. ## How To Determine If a Dynamic Model Has Empty Inputs With MATLAB isempty() Function

The isempty() function is also used to determine if one or more inputs to a dynamic model are empty for simulation. The following is a basic continuous-time state space model “s” created with the ss() function, which we pass as an input argument to isempty():

s = ss ( 1, 5, 2, 2 );

isempty ( s )

As we see in the following figure, isempty() returns a logical 0 since inputs A, B, C, and D of model “s” are not empty: In the following snippet, we will create a model with empty inputs and pass it as an input argument to isempty().

s = ss ( 1, [] , 4, [] );

isempty ( s )

In this model, arrays B and C are empty, so isempty() returns a logical 1. ## Conclusion

When we call functions and pass empty arrays in their input arguments, they can throw exceptions or unexpected results. The isempty() function belongs to the family of “is…..” functions, a set of functions that use a logical output to determine the type of data in an array. This MATLAB article showed you how to use the isempty() function to determine whether a scalar, vector, matrix, or table is empty. We have seen a complete description of the function, its input and output arguments, and the data type it accepts. To better learn how to use this function, we have prepared several practical examples with different types of inputs and have shown all the ways to use this function through code snippets and images.

We hope that this MATLAB article was helpful for you. See other Linux Hint articles for more tips and information.

### About the author #### Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).