Matlab

The isnumeric Function in MATLAB

After knowing the data type, we will send in the input arguments of a function, which is essential in programming. It can avoid errors or unexpected behavior due to data incompatibility.

In this Linux Hint article, you will learn how to use the MATLAB isnumeric() function to determine if a matrix is numeric. We explain everything you need to know about using this function: its input and output arguments, the type of data it accepts, etc.

In addition, you will find pictures and practical examples with code snippets and function calls with different data types and arrays.

MATLAB isnumeric() Function Syntax

r = isnumeric ( x )

Description of the MATLAB isnumeric() Function

The isnumeric() function returns a logical result in “r” equal to 1 if the scalar “x” or all elements of the vector or matrix of its input arguments have numeric values. A logical 0 will result if the scalar or any element of a vector or matrix NOT has non-numeric data.

The “is….” functions are a set of MATLAB functions for determining the data type of an array.

How To Determine if a Scalar is Numeric With MATLAB’s isnumeric() Function

In this example, we will see how to use the isnumeric() function to determine if a scalar is numeric. To do this, we use the MATLAB command console and create the scalar “x” by assigning it a value, character, or another data type. Then, we call the isnumeric() function and send “x” as the input argument. The output “r” is the logical value 1 if it is numeric and 0 if it is not. The following image is a code snippet that shows the results when we send a numeric value and a character as input arguments:

x = 88;

r = isnumeric ( x )

x = 'a';

r = isnumeric ( x )

How To Determine if Values in a Vector are Numeric With MATLAB’s isnumeric() Function

Now, we will see how to determine if the values in the elements of a vector are numeric. To do this, we create the vector “x” on the command line and call the Isnumeric() function as follows:

x = [ 8, -9, 58, 62, 0, 77 ];

r = isnumeric ( x )

As shown in the following figure, isnumeric() returns a single logical result determined by all vector elements:

How To Determine if Values in an Array are Numeric With MATLAB’s isnumeric() Function

In this example, we will see how to determine if the values in the elements of an array are numeric. To do this, we create the array “x” on the command line and call the Isnumeric() function.

x = [ 8, -9, 58, 62 ;

55, 84, NaN, 99 ;

44, 'j', 56, 'o';

74, 14, inf, NaN ];

r = isnumeric ( x )

As shown in the following figure, isnumeric() returns a single logical result determined by all array elements:

The MATLAB isnumeric Function in Conditionals If

As explained previously in this article, these functions are widely used in conditional expressions since they allow us or not allow us to execute operations according to the data type of a certain array, which avoids data compatibility errors. This can be seen in the code for many functions in the MATLAB library where the specific processing performed depends on the type of data sent in the input arguments.

In this example, we will create a function that performs two basic operations, the division of a by b, which also returns the result of this operation on the one hand, and the remainder after the division on the other, which is a function with two inputs and two outputs. Next, we will see the code of this function.

function [ d, r ] = expl_1 ( a, b )

d = a ./ b;

r = mod ( a, b );

end

Create a script, paste this snippet, and save it as “expl_1”. Then, from the MATLAB command line, enter different data types in “a” and “b” and call the “expl_1” function.

As shown in the previous figure, the division operations and the mod() function give the following error messages when we enter non-numeric data such as strings in a or b.

“Operator ‘./’ is not supported for type ‘string’ operands.”

“Check for incorrect argument data type or missing argument in the call to function ‘mod’.”

The best solution to avoid these errors or unexpected results is to use conditionals at the beginning of the function where the data type condition determines whether the function continues to execute or not. In the following code, we see the solution to this problem by implementing the “if” conditional with an AND short-circuit logic, where the function proceeds with the execution of the mathematical expressions only if “a” and “b” contain numeric values.

function [ d, r ] = expl_1 ( a, b )

if isnumeric ( a ) && isnumeric ( b )

% It only runs if "a" AND "b" are numeric.

d = a ./ b;

r = mod ( a, b );

end

end

Now we replace the code of the expl_1 function with the one below.

We can also invert the result of isnumeric() and use short-circuit logic “OR” to set up a return in the function before performing the mathematical operations that would generate an error. Next, let us look at the code for generating the conditional return.

function [ d, r ] = expl_1 ( a, b )

if ~isnumeric( a ) || ~isnumeric( b )

% If "a" OR "b" are NOT numeric, the function returns.

return;

end

% Only executed if "a" AND "b" are numeric

d = a ./ b;

r = mod ( a, b );

end

With these two ways of using isnumeric() in ” if ” conditions, we achieve that the expl_1 function performs the mathematical operations only if the input data are compatible.

Conclusion

As we have seen, this function is an excellent addition to conditionals, allowing us to execute code only when the data type is compatible. This is very useful when we create our functions to execute mathematical formulas, as we can filter the input data and only process it if the data is numeric. In this article, we have explained how to use isnumeric() in MATLAB. We have given a complete description of the function, its syntax, structure, inputs, outputs, data types, and arrays it accepts. We hope you found this MATLAB article helpful. 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).