Matlab

# The deg2rad Function in MATLAB

Like all programming languages, MATLAB has several functions for converting data from one type to another.

Most of the functions MATLAB provides for signal analysis and wave generation have input arguments expressed in radians.

However, in practice, it is sometimes better to perform calculations based on degrees. Therefore, the deg2rad() function is a useful tool for converting these units.

Below we will explain in detail everything about this function, its syntax, calling modes, input and output arguments and accepted data types. We have also included pictures and practical examples in this article that show how you can use this function.

r = deg2rad ( d )

## Description and Examples for MATLAB deg2rad() Function

The deg2rad() function is used to convert angles expressed in degrees to radians. This function converts the degrees sent in “d” to radians and returns the result in “r”. Deg2rad() accepts scalars, vectors, and matrices as input arguments.. This function accepts scalars, vectors and matrices as input arguments. In cases where the conversion is done using matrices or vectors, deg2rad() returns in “r” a matrix or vector of the same size as sent in “d”. Although using this function is useful in practice, there are several ways to convert degrees to radians. One of them is to use the following formula.

The function deg2rad() also works with complex numbers. In cases where “d” contains complex numbers, the conversion of the real and imaginary parts is done separately. Next, we will look at some examples where we implement this function.

## How to Convert a Scalar from Degrees to Radians with MATLAB deg2rad() Function

In this example we will show you how to convert a scalar expressed in degrees to a scalar expressed in radians with the deg2rad() function. For this we will create the scalar “deg” that contains a degrees value and we will send it as the input argument of this function.

deg = 165;

As seen in the figure below, rdeg2rad() returns the scalar “rad” with the value of “deg” converted to radians. ## How to Convert a Vector with Units of Measure Expressed in Degrees to a Vector Expressed in Radians with the MATLAB deg2rad() Function

In this example, we will see how to convert the vector “deg” with values expressed in degrees to a vector “rad” of the same size with the values of “deg” converted to. For this, we are going to create a vector of 8 elements and we will send them as the input argument of the function. Below we can see the code for this conversion.

deg = [ 0, 45, 90, 135, 180, 225, 270, 360 ];

As can be seen in the following figure, deg2rad() in “rad” returns a vector of the same size as “deg” with the angle values expressed in radians. ## How to Convert Angle Measures in Degrees to Radians in Scalars with Complex Numbers Using the MATLAB Function deg2rad()

In this example, we will see how to convert angular measures expressed in complex numbers. When we use this function to convert a complex number, deg2rad() converts the real and imaginary parts separately. Next, let us look at the code snippet to get this conversion.

deg = 13.2374 + 3.2458i;

As shown in the following figure, deg2rad() returns in “rad” a scalar with the complex value of the angle converted and expressed in radians. ## How to Convert an Array with Elements Representing Angle Values Expressed in Degrees to an Array with Angle Values Expressed in Radians Using the MATLAB Function deg2rad()

In this example, we will see how to convert an array of angle values in degrees to an array of those values in radians using MATLAB’s deg2rad() function. To do this, we create a 3 x 3 array of elements with angle values in degrees. We then call the function and pass this array as the input argument. The method of the function call is the same as in the previous examples.

deg =[  0,  45,  90;
135, 180, 225;
270, 315, 360];

As the figure shows, deg2rad() returns an array of the same size as “deg” with the values converted to radians. ## How to Make an Application to do Conversions from Degrees to Radians with the MATLAB deg2rad() Function.

In practice, many engineers or programmers prefer to express angle measurements in degrees because, for example, data sheets for electronic devices use degrees as the unit of measurement in their equations.

In this example, we will create a simple console application to convert degrees to radians. In this application, we will use the prompt() function to prompt the user to enter a value expressed in degrees. This data will be input using the input() function and converted to radians using the deg2rad() function. Once the data is converted, we display it in the command console using the disp() function.

Below is the full script of this console application. Create a script, paste it and run “Run”. To close the application, press Ctrl+c.

while 1
prompt = 'Enter the value expressed in degrees';
deg=input(prompt);
end

The following image shows the application running in the MATLAB environment. ## Conclusion

In this article, we explained how to use the deg2rad() function to convert angular units of measure in MATLAB. This function is widely used to complement the tools that this powerful scientific calculation language provides for analyzing and generating signals and waves with different shapes. To help you better understand what this function is all about, we have included practical examples with code fragments and images showing the implementation of this function in the Matlab environment. We have also created a simple console application that is a handy tool for converting these units of measurement. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information. 