learnonline

MATLAB Short Course

2. Simple Mathematics

Assignment statements.

The preceding MATLAB commands that assign the value of the expression after the ’=’ sign  to the variable before the ’=’ sign are assignment statements. Note that all variables in the expression after the ’=’ sign must have previously been allocated a value, or else an error occurs. For example, enter the following commands:

c=sqrt(a^2+b^2)

You will see the error message ??? Undefined function or variable ’b’ .  Consider the following:

The last line has nothing at all to do with a mathematical equation. It is a MATLAB  assignment statement that calculates x 2 −12 at x = 7 and stores the result in the variable x , thereby over-writing the previous value.

Browse Course Material

Course info.

  • Yossi Farjoun

Departments

  • Mathematics

As Taught In

  • Programming Languages
  • Computational Modeling and Simulation
  • Applied Mathematics

Learning Resource Types

Introduction to matlab programming.

Often, a result of some calculation is needed for later use, or perhaps a complicated expression can be examined more carefully if done in parts. Both can be done by the use of “variables”. Variables hold whatever result you put in them by the use of the equal sign ( = ):

  • x=1 creates a variable called “x” and stores the value “1” in it. If one then types “x” in an expression , MATLAB® will use the value stored in “x”, i.e., “1”.
  • Similarly one can define variables to hold anything that MATLAB can calculate.
  • You can easily overwrite a variable with a new assignment: x=2 now the variable x “contains” the value “2”.
  • One can use x as part of an expression: x^2+x-cos(x)
  • Or to create a new variable: y= x^2+7
  • A variable can be a vector (or matrix): A= [1 2 3 4]
  • One can change just a part of A : A(3)= 0

In this last example, we are getting ahead of ourselves by referring to an element of a vector. We will touch on that more later.

Note that you can “hide” important MATLAB functions and constants by defining a variable with the same name: pi=3 will give interesting results later (to remove clear pi ). This is usually not a good idea, so take care before using a nice name like sum , exp , or det , as these are all built-in functions. You can check if a variable is already in use by using the which command:

tells us that pi is a built-in function, while

tells us that Pi is unused. The difference is in the capitalization. MATLAB-defined functions will always use lower-case names (even if the helpfile will show these as all CAPITAL), which implies that you can always avoid collision by capitalizing the fiirst letter of your variable and functions names.

  • Let x=1 and y=2 . Exchange the values of these two variables without specif­ically using ‘1’ or ‘2’ i.e., the exchange should work regardless of the values held by the variables. Hint: You can invent a new variable. Another Hint: Imagine you have misplaced your kids’ breakfast and now Tom’s Cornflakes are in Sally’s bowl and Sally’s CocoPuffs are in Tom’s bowl. You have already poured the milk, how can you fix the problem without throwing away and starting over?
  • Repeat some of the previous exercises using variables.

facebook

You are leaving MIT OpenCourseWare

MATLAB® and Its Applications in Engineering: [Based on MATLAB 7.5 (R2007b)] by Raj Kumar Bansal, Ashok Kumar Goel, Manoj Kumar Sharma

Get full access to MATLAB® and Its Applications in Engineering: [Based on MATLAB 7.5 (R2007b)] and 60K+ other titles, with a free 10-day trial of O'Reilly.

There are also live events, courses curated by job role, and more.

2.8. Assignment Statement

General form of an assignment statement is given as follows:

When an assignment statement is executed, the value of the expression to the right of the equality sign is first computed and the result obtained is assigned to the variable mentioned on the left of the equality sign.

Example 2.5.

Get MATLAB® and Its Applications in Engineering: [Based on MATLAB 7.5 (R2007b)] now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

Don’t leave empty-handed

Get Mark Richards’s Software Architecture Patterns ebook to better understand how to design components—and how they should interact.

It’s yours, free.

Cover of Software Architecture Patterns

Check it out now on O’Reilly

Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems every day.

variable assignment statement matlab

Library homepage

  • school Campus Bookshelves
  • menu_book Bookshelves
  • perm_media Learning Objects
  • login Login
  • how_to_reg Request Instructor Account
  • hub Instructor Commons
  • Download Page (PDF)
  • Download Full Book (PDF)
  • Periodic Table
  • Physics Constants
  • Scientific Calculator
  • Reference & Cite
  • Tools expand_more
  • Readability

selected template will load here

This action is not available.

Engineering LibreTexts

1.4.1: More About Valid Variable Names

  • Last updated
  • Save as PDF
  • Page ID 87272

  • Carey Smith
  • Oxnard College

Variable Name Rules

MATLAB variable names must start with a letter. The rest of the name can be letters, numbers or the underscore symbol. The letters can be upper or lower case. These are valid variable names:

b, B, Basketball, Basket_Ball, basket5ball, basketBall_12345

These are not valid names:

Basket-Ball, Basket_Ball!, Basket?Ball, Basket Ball.

Descriptive names are encouraged, in order to make the code readable. So 'pressure' and 'press' are better names than just 'p'. Names are allowed to be very long, but they should not be more than a quarter of the command window, so keep them to less than 20 letters, unless there is a very good reason to make them longer. (The maximumn allowed name length is 63 characters.)

Sometimes, you may have 2 version of the same quantity, such as a distance in both inches and centimeters. A good practice is to give them related names with identifiers, such as dist_inch and dist_cm, so that you always know which units you are using.

Variable Names to Avoid

Don't use the name of a function. For example, don't use these names:

sqrt, sin, cos, log10, sum.

Also, don't use 2 names that only differ for upper vs. lower case letters; that can lead to confusion. For example, having 2 variables named kg and Kg should be avoided.

Exercise \(\PageIndex{1}\) Good Variable Names

Which of the following are good variable names? Make a decision about each before looking at the answers.

You may enter each in the command window by setting the name = 1, in order to see if it is valid.

You may use the exist() function to see if it is a function name.

A-very_long_name

Even_123_longer456_name_9876543210

sind -- a function name--don't use it

sinx -- valid

A-very_long_name -- not valid becuz of the -, which is interpreted as a minus sign

Even_123_longer456_name_9876543210 -- valid

important! -- nor valid becuz of the !

_next -- not valid. It can't start with an underscore

1one1 -- not valid. It can't start with a number

MATLAB keywords

It is not allowed to use MATLAB keywords. These are words that MATLAB reserves for program structure. This is the list of keywords:

'break' 'case' 'catch' 'classdef' 'continue' 'else' 'elseif' 'end' 'for' 'function' 'global' 'if' 'otherwise' 'parfor' 'persistent' 'return' 'spmd' 'switch' 'try' 'while'

Help Center Help Center

  • Help Center
  • Trial Software
  • Product Updates
  • Documentation

Logical (Boolean) Operations

MATLAB ® represents Boolean data using the logical data type. This data type represents true and false states using the numbers 1 and 0 , respectively. Certain MATLAB functions and operators return logical values to indicate fulfillment of a condition. You can use those logical values to index into an array or execute conditional code. For more information, see how to Find Array Elements That Meet a Condition .

This example shows how to filter the elements of an array by applying conditions to the array.

This example shows how to use the any and all functions to reduce an entire array to a single logical value.

Precedence rules determine the order in which MATLAB evaluates an expression.

Related Information

  • MATLAB Operators and Special Characters

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

IMAGES

  1. MATLAB Variable (Assign value, string Display, multiple Variables & Rules)

    variable assignment statement matlab

  2. Variables in MATLAB Command Window

    variable assignment statement matlab

  3. Matlab Variables

    variable assignment statement matlab

  4. How to define variables and assign values to them in the matlab

    variable assignment statement matlab

  5. 2.1 Variable assignment in MATLAB

    variable assignment statement matlab

  6. Declaration of Variables in MATLAB

    variable assignment statement matlab

VIDEO

  1. Chapter 1 Sample Program

  2. Assignment Statement and Constant Variable

  3. SCIENTIFIC COMPUTING USING MATLAB WEEK 3 ASSIGNMENT-3 ANSWERS #NPTEL #WEEK-3 #ANSWERS #2024

  4. 6 storing values in variable, assignment statement

  5. # video 7 matlab tentang if statement

  6. Combining a Variable Definition and Assignment Statement

COMMENTS

  1. Assign value to variable in specified workspace

    To assign values in the MATLAB base workspace, use 'base'. The base workspace stores variables that you create at the MATLAB command prompt, including any variables that scripts create, assuming that you run the script from the command line or from the Editor. To assign variables in the workspace of the caller function, use 'caller'. The caller ...

  2. Create and Edit Variables

    You can create new variables in the workspace by running MATLAB code or using existing variables. To create a new variable, enter the variable name in the Command Window, followed by an equal sign (=) and the value you want to assign to the variable. For example, if you run these statements, MATLAB adds the four variables x, A, I, and C to the ...

  3. Assignment of variables by comma separated lists

    Assignment of variables by comma separated lists. Learn more about variables MATLAB. ... which states "You also can enter more than one statement on the same line by separating statements. To distinguish between commands, end each one with a comma or semicolon. Commands that end with a comma display their results, while commands that end with a ...

  4. 1.4: Variables and Assignment Statements

    Assignment Statements. You can create your own variables, and give them values, with an assignment statement. The assignment operator is the equals sign (=), used like so: >> x = 6 * 7 x = 42. This example creates a new variable named x and assigns it the value of the expression 6 * 7. MATLAB responds with the variable name and the computed value.

  5. Assignment vs Equality

    Assignment Statement. An assignment statement is used to assign a value to a variable name. Once the value has been assigned, the variable name can be used instead of the value. Matlab allows a variable to be assigned to a single scalar value, or an array of values. You will learn more about arrays in later lessons. The = sign is the assignment ...

  6. MATLAB: Assignment statements

    The preceding MATLAB commands that assign the value of the expression after the '=' sign to the variable before the '=' sign are assignment statements. ... It is a MATLAB assignment statement that calculates x 2 −12 at x = 7 and stores the result in the variable x, thereby over-writing the previous value. Previous.

  7. 2.1 Variable assignment in MATLAB

    This video explains the definition of variable assignment in MATLAB and what is the rules to name a variable in MATLAB. In addition, How to defined multiple ...

  8. MATLAB Variable (Assign value, string Display, multiple Variables & Rules)

    In MATLAB, an assignment statement automatically generates variable. Assign the value to a Variable. An assignment statement used for assigning a value to a variable. The assignment statement generally having the symbol = which is known as the assignment operator. Example: The statement in MATLAB is given as. x=10; fprintf('%d\n',x) Output: 10 ...

  9. How do I do multiple assignment in MATLAB?

    So the following works as expected: > [x,y] = deal(88,12) x = 88. y = 12. The syntax c{:} transforms a cell array in a list, and a list is a comma separated values, like in function arguments. Meaning that you can use the c{:} syntax as argument to other functions than deal. To see that, try the following:

  10. Variables

    If one then types "x" in an expression, MATLAB® will use the value stored in "x", i.e., "1". Similarly one can define variables to hold anything that MATLAB can calculate. You can easily overwrite a variable with a new assignment: x=2 now the variable x "contains" the value "2". One can use x as part of an expression: x^2+x ...

  11. Variable Assignment

    Variable Assignment. This works just like storing values as variables on your calculator, except it is much easier with MATLAB (and a full keyboard). We say that the variable x has been assigned the value 42, or that the value 42 is now "stored in x ". Notice that a new entry has appeared in the workspace corresponding to this variable assignmetn.

  12. Comma-Separated Lists

    You can assign any or all consecutive elements of a comma-separated list to variables with a simple assignment statement. Define the cell array Cand assign the first row to variables c1 through c6. C = cell ... MATLAB assigns C{1,1:3} to the variables c1, c2, and c3 and ignores C{1,4:6}.

  13. Assignment Statement

    2.8. Assignment Statement General form of an assignment statement is given as follows: variable_name = expression; When an assignment statement is executed, the value of the expression to the right … - Selection from MATLAB® and Its Applications in Engineering: [Based on MATLAB 7.5 (R2007b)] [Book]

  14. 1.4.1: More About Valid Variable Names

    Variable Name Rules. MATLAB variable names must start with a letter. The rest of the name can be letters, numbers or the underscore symbol. The letters can be upper or lower case. These are valid variable names: b, B, Basketball, Basket_Ball, basket5ball, basketBall_12345. These are not valid names:

  15. Conditional Statements

    Conditional Statements. Conditional statements enable you to select at run time which block of code to execute. The simplest conditional statement is an if statement. For example: % Generate a random number. a = randi(100, 1); % If it is even, divide by 2. if rem(a, 2) == 0. disp('a is even')

  16. Matlab conditional assignment

    23. One line conditional assignment: a(a > 5) = 2; This is an example of logical indexing, a > 5 is a logical (i.e. Boolean or binary) matrix/array the same size as a with a 1 where ever the expression was true. The left side of the above assignment refers to all the positions in a where a>5 has a 1.

  17. Logical (Boolean) Operations

    MATLAB ® represents Boolean data using the logical data type. This data type represents true and false states using the numbers 1 and 0, respectively. Certain MATLAB functions and operators return logical values to indicate fulfillment of a condition. You can use those logical values to index into an array or execute conditional code.