EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Variables

Matlab Variables

Priya Pedamkar
Article byPriya Pedamkar

Updated June 30, 2023

Matlab Variables

Introduction to Matlab Variables

The variables created in MATLAB code are managed by its workspace, which is responsible for handling memory locations and storing the values assigned to each respective variable. Matlab workspace supports creating new variables and reusing existing variables in command execution. In the Matlab environment, each variable is treated as a matrix or an array, irrespective of their data types.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Declaring variables in Matlab

Matlab does not need variables to be declared explicitly. When the user specifies a variable name followed by an equation and data, the system instantiates a variable with the given name, holding the assigned data within it.

The data can be assigned to a variable in ways:

1. Assigning a constant value to the variable

Code:

var1 = 15.41
var2 = [10 22 43; 40 45 63; 27 48 19]

Output:

Matlab Variables1

2. Assigning an expression to a variable

Code:

var1 = sqrt(16)
var2=sin(1)

Output:

Matlab Variables2

3. Initializing variables in Matlab

Matlab variables support both empty and non-empty data as initial values.

Code:

var1 = [] var2=10

Output:

Matlab Variables3

How do Variables work in Matlab?

Matlab adds the variables to the workspace once a variable is created from the code.

Code:

var1="I am variable";
I = [1 2 3; 4 5 6; 7 8 9];

Output:

Matlab Variables4

Explanation: The content of the variable can be edited using a command or can be edited from the variable editor. Other operations, such as deleting, duplicating, copying, resizing, or reshaping the variable, can be done in the workspace.

Code:

var1=50;var2=60;var3=70;

Output:

command window

Matlab Variables6

Types of variables in Matlab

Various data types are supported by Matlab variables, such as:

1. Char

Matlab variables support the array of characters.

Code:

char_var = "I am a char type variable";
char_var_arr = ["charset1","charset2","charset3"];

Output:

Char

Matlab Variables8

2. Complex

The complex variables i.e. in the form of “real +i*imaginary” are supported by Matlab variables.

a. Creating a complex number:

Code:

complex_var = complex(5,10);

Output:

Complex

b. Creating an array of complex numbers:

Code:

real = uint8([11;25;23;40]);
imgainary = uint8([21;25;27;47]);
complex_arr = complex(real,imgainary)

Output:

Creating an array

3. Double

In Matlab, the default numeric data type or class is ‘double’, which provides high precision for most of the computational tasks. Numeric variables are automatically captured in the form of 64-bit (8-byte) double-precision floating-point values.

Code:

var_name = 100;
xtype = class(var_name)

Output:

Matlab Variables11

It supports variable datatype conversion to double.

Code:

other_var = true
other_var_double = double(other_var);
other_var_newtype = class(other_var_double)

Output:

conversion to double

4. Signed integer

Matlab supports variable arrays of the signed integer data type. The variables can be defined as 8/16/32/64-bit signed integers, depending on the size of the data.

Code:

var_int8 = int8(50) ; var_int16 = int16(50); var_int32 = int32(50); var_int64 = int64(50);

Output:

integer

5. Logical

Logical value can be used in Matlab programming in the form of a variable. But Complex values and NaNs are unsupported for logical value conversion and throw conversion errors.

Any non-zero element gets converted as logical 1 i.e. Boolean true, and 0 gets converted as logical 0 i.e. Boolean false.

Code:

A = [10 -23 0;35 42 71; -28 0 36];
L = logical(A)

Output:

Logical

6. Single

Matlab supports Single-precision variable declaration, and those get stored as ‘single’ datatype 4-byte (32-bit) floating-point values.

Code:

single_var= single(100)
vartype = class(single_var)

Output:

Single

7. Structure

A structure array can be defined as a data type that allows the grouping of related data using data containers which are termed fields and can be used as a user-defined data type. A field can contain data of any Matlab-supported data type of class.

Syntax Description
s = struct create structure in the form of a scalar with size 1X1, with no fields.
s = struct(field,value) creates a structure in the form of an array with the field and value defined for the structure.
s=struct(field1,value1,…,fieldN,valueN)  creates a structure in the form of an array with multiple fields (field1, field2,…, fieldN) and values(value1, value2,…..,valueN) defined for the structure.
s = struct([])  used to create a null structure(0-by-0)  with no fields.
s = struct(obj) used to create a scalar structure consisting of field names and values corresponding to the properties of ‘obj’.

Code:

struct_var = struct

Output:

Matlab Variables16

Code:

field_name = 'field_name';
values = {'A string';
[15, 25, 35];
magic(4)};
s = struct(field_name,values)
s.field_name

Output:

Structure

Code:

field1 = 'field1';  value1 = zeros(1,15);
field2 = 'field2';  value2 = {'str1', 'str2'};
field3 = 'field3';  value3 = {pi/3, pi.^3};
field4 = 'field4';  value4 = {'text'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4);
s.field1
s.field2
s.field3
s.field4

Output:

Matlab Variables18

Code:

s=struct([])

Output:

Matlab Variables19

8. Unsigned Integer

Matlab supports variable arrays of the unsigned integer data type. The variables can be defined as unsigned integers of different sizes, such as 8-bit, 16-bit, 32-bit, or 64-bit, depending on the size of the data that needs to be stored in the variable.

Code:

var_int8 = uint8(50) ; var_int16 = uint16(50); var_int32 = uint32(50); var_int64 = uint64(50);

Output:

Unsigned Integer

9. Fixed point data type

Matlab SIMULINK blocks allow users to create models that use fixed-point datatypenumerical to represent parameter values and signals. The application of fixed-point data reduces memory consumption and improves the speed of code generated from a SIMULINK model.

Code:

var1 = fi(pi)

Output:

Fixed point data type

Note: This default variable ‘ans’ is reusable throughout the code. The elements or their subsets from a multidimensional array and tall arrays are not editable in the Variables editor.

For numeric array variable: ‘0’

For cell and structure array variable: ‘[]’

For categorical variable: ‘<undefined>’

Recommended Articles

This is a guide to Matlab Variables. Here we discuss an introduction to Matlab Variables, how it works, along with types and examples. You can also go through our other related articles to learn more –

  1. Linear Fit Matlab
  2. Covariance in Matlab
  3. Contour plot in Matlab
  4. Matlab Plot Marker
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests
 11+ Hours of HD Videos
5 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more