askvity

How Do You Make a Data Type Double in MATLAB?

Published in MATLAB Data Types 4 mins read

In MATLAB, the primary way to ensure a variable has the double precision data type is by using the built-in double() function. It's important to note that double is also the default numeric data type in MATLAB.

Understanding MATLAB's Default Numeric Type

When you create most numeric variables in MATLAB without specifying a type, they are automatically stored as double precision floating-point numbers.

For example:

  • x = 10; creates x as a double.
  • y = [1.2, 3.4, 5.6]; creates a double array y.
  • z = pi; creates z as a double.

You can always check the data type of a variable using the class() function:

x = 10;
class(x) % Output: 'double'

Converting Data Types to Double Precision

While MATLAB defaults to double, you often need to convert data from other types (like single-precision, integers, or symbolic variables) into double. The double() function serves as the standard conversion tool for this purpose.

Converting Symbolic Variables

As mentioned in the reference, the double(s) syntax is used to convert symbolic values s to double precision.

Reference Insight: "d = double( s ) converts the symbolic values s to double precision. Converting symbolic values to double precision is useful when a MATLAB function does not accept symbolic values."

Symbolic variables are exact representations and are part of the Symbolic Math Toolbox. Many standard MATLAB functions that operate on numbers do not work directly with symbolic types. Converting them to double allows you to use these functions for numerical computation or visualization.

Here's how you convert a symbolic variable to double:

% Requires Symbolic Math Toolbox
syms a b;
sym_expr = a^2 + b^2; % A symbolic expression

% Convert the symbolic expression to double
double_value = double(sym_expr);

Note: Converting a symbolic expression containing variables (a, b in the example above) to double will result in a numeric value of NaN (Not-a-Number) unless the symbolic expression evaluates to a specific number (e.g., sym(pi)). If you have symbolic variables, you often need to substitute values first using subs before converting to double if you expect a specific numeric result.

Converting Other Numeric Types

You can also use the double() function to convert other numeric types, such as single or various integer types (int8, uint16, etc.), into double precision.

  • From single:

    single_num = single(12.34);
    double_num = double(single_num);
    class(double_num) % Output: 'double'
  • From Integers:

    int_num = int32(100);
    double_num_from_int = double(int_num);
    class(double_num_from_int) % Output: 'double'

Converting Character Arrays/Strings Representing Numbers

If you have a character array or string containing a numeric representation, you can often convert it to double using double() or the more specific str2double() function.

char_num = '5.67';
double_from_char = double(char_num); % Converts character *codes*, often not intended
double_from_string_num = double("8.90"); % Converts numeric string representation

% Recommended function for numeric strings:
double_from_string_num_str2double = str2double("12.34");

Summary of Conversions to Double

The double() function is versatile for type conversion in MATLAB.

Original Data Type Example Input Command Resulting Data Type Notes
Default Numeric x = 5; N/A (already double) double MATLAB's default for most numbers
Symbolic Variable/Expr. syms a; double(a) double(s) double Useful for using symbolic values in numeric functions
Single Precision single(3.14) double(single_var) double Converts single to double precision
Integer Types int16(255) double(integer_var) double Converts integer to double precision
Numeric Character Array '123' double('123') double Converts character codes primarily. Use str2double for numeric values.
Numeric String Array "456" double("456") double Converts string representing number

In conclusion, to make a data type double in MATLAB, either rely on MATLAB's default behavior when creating numeric variables, or explicitly convert other data types using the double() function, particularly when working with symbolic variables or other non-double numeric formats.

Related Articles