EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Matlab Variables

By Priya PedamkarPriya Pedamkar

Secondary Sidebar
Matlab Tutorial
  • Basic
    • MATLAB Area Under Curve
    • MATLAB not equal
    • MATLAB max
    • MATLAB exist
    • MATLAB Table
    • MATLAB regression
    • MATLAB Lists
    • MATLAB quantile
    • MATLAB Round
    • MATLAB readtable
    • MATLAB disp
    • MATLAB Standard Deviation
    • MATLAB quadprog
    • MATLAB Transpose
    • Introduction to MATLAB
    • Advantages of MATLAB
    • MATLAB Features
    • Taylor Series MATLAB
    • MATLAB Z Transform
    • fsolve in MATLAB
    • MATLAB QR
    • Career in MATLAB
    • Uses Of MATLAB
    • MATLAB Free
    • How to Install MATLAB
    • How to Use MATLAB?
    • MATLAB Version
    • MATLAB Compiler
    • MATLAB Commands
    • MATLAB Block Comment
    • MATLAB? sprintf
    • MATLAB fprintf
    • Data Types in MATLAB
    • MATLAB Integral
    • MATLAB Double Integral
    • MATLAB boolean
    • MATLAB vpa
    • MATLAB Object
    • MATLAB Annotation
    • MATLAB Variables
    • MATLAB Global Variables
    • MATLAB Operators
    • MATLAB Logical Operators
    • MATLAB nan
    • MATLAB Patch
    • MATLAB AND Operator
    • MATLAB OR Operator
    • Vectors in MATLAB
    • What is Simulink in MATLAB
    • MATLAB Interpolation
    • MATLAB Imread
    • fscanf MATLAB
    • Euler Method MATLAB
    • Root Locus MATLAB
    • MATLAB return
    • Bode Plot MATLAB
    • Nargin MATLAB
    • MATLAB Matrix Inverse
    • MATLAB String to Number
    • MATLAB string
    • MATLAB ColorBar
    • MATLAB Surfc
    • MATLAB Concatenate
    • NUMEL MATLAB
    • MATLAB? File Extension
    • MATLAB File
    • MATLAB Smooth
    • MATLAB ones
    • Exponential in MATLAB
    • MATLAB ksdensity
    • MATLAB log
    • MATLAB Append
    • MATLAB hold on
    • MATLAB Magnitude of Vector
    • Heatmap in MATLAB
    • MATLAB xticks
    • MATLAB randn
  • Control Statements
    • IF-Else Statement in MATLAB
    • If Statement in MATLAB
    • Loops in MATLAB
    • For Loop in MATLAB
    • While Loop in MATLAB
    • do while loop in MATLAB
    • Nested Loop in MATLAB
    • Switch Statement in MATLAB
    • Break in MATLAB
  • Functions
    • MATLAB Functions
    • MATLAB user defined function
    • Calling Functions in MATLAB
    • Transfer Functions in MATLAB
    • Anonymous Functions in MATLAB
    • Inline Functions in MATLAB
    • Bessel Functions in MATLAB
    • Mean Function in MATLAB
    • Find Function MATLAB
    • Filter Function in MATLAB
    • IIR Filter MATLAB
    • Piecewise Function in MATLAB
    • Sum Function in MATLAB
    • Simulink MATLAB Function
    • MATLAB Create Function
    • MATLAB Inverse Function
    • MATLAB Count
    • Step Function MATLAB
    • MATLAB limit
    • Fminsearch in MATLAB
    • Covariance in MATLAB
    • Summation in MATLAB
    • Linear Fit MATLAB
    • MATLAB?linear regression
    • MATLAB Derivative
    • MATLAB Derivative of Function
    • MATLAB Comet()
    • Fzero MATLAB
    • xlabel MATLAB
    • Matplotlib Legend
    • Matplotlib Subplots
    • Plot graph MATLAB
    • MATLAB Format
    • MATLAB plot title
    • Multiple Plots in MATLAB
    • MATLAB Indexing
    • Ceil MATLAB
    • Curve Fitting MATLAB
    • MATLAB trapz()
    • MATLAB Normalize
    • MATLAB diff
    • MATLAB sym()
    • MATLAB Syms
    • Absolute Value MATLAB
    • MATLAB Exponential
    • Kalman Filter MATLAB
    • Low Pass Filter MATLAB
    • Bandpass Filter MATLAB
    • MATLAB Unique
    • Trapezoidal Rule MATLAB
    • MATLAB Root Finding
    • MATLAB stem()
    • MATLAB loglog()
    • MATLAB Autocorrelation
    • MATLAB Sort
    • Simplify MATLAB
    • Cumsum MATLAB
    • Eval Function MATLAB
    • Polyval MATLAB
    • MATLAB Colon
    • MATLAB Eigenvalues
    • MATLAB fit
    • Delta Function MATLAB
    • MATLAB Remainder
    • Differentiation in MATLAB
    • Permute MATLAB
    • isempty MATLAB
    • MATLAB text()
    • MATLAB Display Text
    • Varargin in MATLAB
    • MATLAB gca
    • MATLAB fill()
    • MATLAB pcolor()
    • MATLAB min
    • MATLAB xcorr
    • MATLAB? color codes
    • Semilogy MATLAB
    • MATLAB? eye
    • feval MATLAB
    • num2str in MATLAB
    • MATLAB Images
    • MATLAB Image? Segmentation
    • Imagesc MATLAB
    • MATLAB Image Processing
    • MATLAB Image Resize
    • MATLAB Flag
    • MATLAB fopen
    • Strcmp MATLAB
    • MATLAB fwrite
    • MATLAB fft()
    • MATLAB zeros()
    • MATLAB textread
    • Arctan MATLAB
    • MATLAB Scripts
    • Butterworth filter MATLAB
    • MATLAB Findpeaks
    • MATLAB find Index
    • MATLAB Cell
    • MATLAB Unit Step Function
    • MATLAB Backslash
    • MATLAB Mod
    • Size Function in MATLAB
    • Secant MATLAB
  • Matrix
    • Matrix in MATLAB
    • 3D Matrix in MATLAB
    • Transpose Matrix MATLAB
    • Sparse Matrix in MATLAB
    • Matrix Multiplication in MATLAB
    • Identity Matrix in MATLAB
    • MATLAB?writematrix
  • Advanced
    • MATLAB Class
    • Arrays in MATLAB
    • Matlab find value in array
    • MATLAB Grader
    • Power Spectral Density MATLAB
    • Matlab Textscan
    • String Array in MATLAB
    • MATLAB Random Numbers
    • Matlab Dot
    • MATLAB 2D Array
    • MATLAB? zero padding
    • MATLAB sort matrix
    • MATLAB Plot Function
    • 2D Plots in MATLAB
    • 3D Plots in MATLAB
    • MATLAB Fread
    • Spectrogram MATLAB
    • MATLAB Average
    • MATLAB exponent
    • MATLAB not enough input arguments
    • MATLAB comment
    • MATLAB zpk
    • Scatter Plots in MATLAB
    • MATLAB 3d scatter plot
    • Bar Graph in MATLAB
    • Bar Plot MATLAB
    • Log Plot MATLAB
    • Polar Plot in MATLAB
    • Surface Plot in MATLAB
    • MATLAB Plot Circle
    • Boxplot in MATLAB
    • MATLAB Plot Multiple Lines
    • Linspace MATLAB
    • Histogram in MATLAB
    • Plot Vector MATLAB
    • MATLAB Legend
    • MATLAB Plot Legend
    • MATLAB ezplots
    • Column Vector MATLAB
    • MATLAB Plot Marker
    • MATLAB LineWidth
    • MATLAB Line Style
    • Contour plot in MATLAB
    • MATLAB Sine Wave
    • Reshape in MATLAB
    • Natural Log in MATLAB
    • Random Number Generator in MATLAB
    • Complex Numbers in MATLAB
    • MATLAB Figure
    • Heatmap in MATLAB
    • MATLAB Technical Computing
    • Colors in MATLAB
    • Colormap in MATLAB
    • MATLAB Plot Colors
    • MATLAB fplot()
    • MATLAB Stacked Bar
    • MATLAB sphere()
    • MATLAB cylinder()
    • MATLAB mesh()
    • Pie Chart in MATLAB
    • MATLAB Gradient
    • Grid on MATLAB
    • Repmat in MATLAB
    • dlmread in MATLAB
    • Meshgrid in MATLAB
    • MATLAB Struct
    • MATLAB Cross Product
    • MATLAB colorbar Label
    • MATLAB Save Variable
    • MATLAB Saveas
    • MATLAB Cell Array
    • Polynomial in MATLAB
    • ismember MATLAB
    • Heaviside MATLAB
    • MATLAB rref
    • MATLAB polyfit()
    • MATLAB xlim
    • MATLAB Variance
    • Optimset MATLAB
    • Quiver MATLAB
    • Newton Raphson MATLAB
    • Mat2cell MATLAB
    • Magnitude MATLAB
    • format long MATLAB
    • Dot Product MATLAB
    • Jacobian MATLAB
    • What is Matlab?
    • Convolution MATLAB
    • Moving Average MATLAB
    • Fourier Series MATLAB
    • Gaussian Fit MATLAB
    • Bisection Method MATLAB
    • Laplace Transform MATLAB
    • Fourier Transform MATLAB
    • Signal Processing MATLAB
    • MATLAB Forms
    • Complex Conjugate MATLAB
    • MATLAB Write to File
    • uigetfile MATLAB
    • MATLAB Toolbox
    • MATLAB Errorbar
    • MATLAB Index Exceeds Matrix Dimensions
    • Nyquist MATLAB
    • Impulse Response MATLAB
    • xlsread MATLAB
    • MATLAB xlswrite
    • Matplotlib Scatter
    • MATLAB Import Data
    • MATLAB Export Data
    • MATLAB Read CSV
  • Programs
    • Square Root in MATLAB
    • Square Wave MATLAB
    • Squeeze MATLAB
    • Factorial in MATLAB
    • Cell to String MATLAB
  • Interview Questions
    • MATLAB Interview Questions

Related Courses

MATLAB Certification Course

R Programming Course

All in One Data Science Courses

Home Data Science Data Science Tutorials Matlab Tutorial Matlab Variables

Matlab Variables

Introduction to Matlab Variables

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

Declaring variables in Matlab

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,938 ratings)

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 are initialized with the initial value assigned to it. 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?

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

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 command or can be edited from the variable editor. Apart from editing, the other operations such as deleting, duplicating, copying, resizing, or reshaping the variable can be done in the workspace.

Multiple variables can be declared in a single command line from the Matlab command window.

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

The array of characters is supported by Matlab variables.

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 captured in the form of 64-bit (8-byte) double-precision floating-point value automatically.

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 that of the singed integer data type. The variables can be defined as 8/16/32/64-bit signed integer 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 not supported for logical value conversion and throws conversion error.

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 grouping related data using data containers which are termed as fields and can be used as a user-defined data type. A field can contain data of any Matlab supported data type of class.

The data of a field can be accessed using dot notation form as given as ‘structName.fieldName’.

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 that of the unsigned integer data type. The variables can be defined as 8/16/32/64-bit unsigned integer depending on the size of the data 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 that is generated from a SIMULINK model.

Code:

var1 = fi(pi)

Output:

Fixed point data type

Note: When the return value from an expression is not assigned to any variableexplicitly, a default variable ‘ans’ gets defined by the system and the return value of the expressions gets assigned to it. 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. In the Workspace browser or the Variables editor, double quotes need to be used surrounding the string value while editing any string type variable.

If an empty variable is created or variable gets empty value during run time, the variable will be assigned to a default value based on the data type such as:

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 does it work, 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
Popular Course in this category
MATLAB Training (3 Courses, 1 Project)
  3 Online Courses |  1 Hands-on Project |  8+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

R Programming Training (13 Courses, 20+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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

Special Offer - MATLAB Training (3 Courses, 1 Project) Learn More