• MATLAB Answers
  • File Exchange
  • AI Chat Playground
  • Discussions
  • Communities
  • Treasure Hunt
  • Community Advisors
  • Virtual Badges
  • Trial software

You are now following this question

  • You will see updates in your followed content feed .
  • You may receive emails, depending on your communication preferences .

Problem with my code?

GEORGIOS BEKAS

Direct link to this question

https://www.mathworks.com/matlabcentral/answers/486213-problem-with-my-code

   0 Comments Show -2 older comments Hide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (2)

Star Strider

Direct link to this answer

https://www.mathworks.com/matlabcentral/answers/486213-problem-with-my-code#answer_397092

Kevin Phung

https://www.mathworks.com/matlabcentral/answers/486213-problem-with-my-code#answer_397094

   1 Comment Show -1 older comments Hide -1 older comments

madhan ravi

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486213-problem-with-my-code#comment_757997

  • matlab function

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

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: .

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)
  • 简体中文 Chinese
  • 日本 Japanese (日本語)
  • 한국 Korean (한국어)

Contact your local office

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Octave: error: value on right hand side of assignment is undefined #2436

@andrescodas

andrescodas commented May 17, 2019

  • 👍 1 reaction

No branches or pull requests

@andrescodas

  • Skip to main content
  • Skip to search
  • Skip to select language
  • Sign up for free
  • Português (do Brasil)

Destructuring assignment

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

Description

The object and array literal expressions provide an easy way to create ad hoc packages of data.

The destructuring assignment uses similar syntax but uses it on the left-hand side of the assignment instead. It defines which values to unpack from the sourced variable.

Similarly, you can destructure objects on the left-hand side of the assignment.

This capability is similar to features present in languages such as Perl and Python.

For features specific to array or object destructuring, refer to the individual examples below.

Binding and assignment

For both object and array destructuring, there are two kinds of destructuring patterns: binding pattern and assignment pattern , with slightly different syntaxes.

In binding patterns, the pattern starts with a declaration keyword ( var , let , or const ). Then, each individual property must either be bound to a variable or further destructured.

All variables share the same declaration, so if you want some variables to be re-assignable but others to be read-only, you may have to destructure twice — once with let , once with const .

In many other syntaxes where the language binds a variable for you, you can use a binding destructuring pattern. These include:

  • The looping variable of for...in for...of , and for await...of loops;
  • Function parameters;
  • The catch binding variable.

In assignment patterns, the pattern does not start with a keyword. Each destructured property is assigned to a target of assignment — which may either be declared beforehand with var or let , or is a property of another object — in general, anything that can appear on the left-hand side of an assignment expression.

Note: The parentheses ( ... ) around the assignment statement are required when using object literal destructuring assignment without a declaration.

{ a, b } = { a: 1, b: 2 } is not valid stand-alone syntax, as the { a, b } on the left-hand side is considered a block and not an object literal according to the rules of expression statements . However, ({ a, b } = { a: 1, b: 2 }) is valid, as is const { a, b } = { a: 1, b: 2 } .

If your coding style does not include trailing semicolons, the ( ... ) expression needs to be preceded by a semicolon, or it may be used to execute a function on the previous line.

Note that the equivalent binding pattern of the code above is not valid syntax:

You can only use assignment patterns as the left-hand side of the assignment operator. You cannot use them with compound assignment operators such as += or *= .

Default value

Each destructured property can have a default value . The default value is used when the property is not present, or has value undefined . It is not used if the property has value null .

The default value can be any expression. It will only be evaluated when necessary.

Rest property

You can end a destructuring pattern with a rest property ...rest . This pattern will store all remaining properties of the object or array into a new object or array.

The rest property must be the last in the pattern, and must not have a trailing comma.

Array destructuring

Basic variable assignment, destructuring with more elements than the source.

In an array destructuring from an array of length N specified on the right-hand side of the assignment, if the number of variables specified on the left-hand side of the assignment is greater than N , only the first N variables are assigned values. The values of the remaining variables will be undefined.

Swapping variables

Two variables values can be swapped in one destructuring expression.

Without destructuring assignment, swapping two values requires a temporary variable (or, in some low-level languages, the XOR-swap trick ).

Parsing an array returned from a function

It's always been possible to return an array from a function. Destructuring can make working with an array return value more concise.

In this example, f() returns the values [1, 2] as its output, which can be parsed in a single line with destructuring.

Ignoring some returned values

You can ignore return values that you're not interested in:

You can also ignore all returned values:

Using a binding pattern as the rest property

The rest property of array destructuring assignment can be another array or object binding pattern. The inner destructuring destructures from the array created after collecting the rest elements, so you cannot access any properties present on the original iterable in this way.

These binding patterns can even be nested, as long as each rest property is the last in the list.

On the other hand, object destructuring can only have an identifier as the rest property.

Unpacking values from a regular expression match

When the regular expression exec() method finds a match, it returns an array containing first the entire matched portion of the string and then the portions of the string that matched each parenthesized group in the regular expression. Destructuring assignment allows you to unpack the parts out of this array easily, ignoring the full match if it is not needed.

Using array destructuring on any iterable

Array destructuring calls the iterable protocol of the right-hand side. Therefore, any iterable, not necessarily arrays, can be destructured.

Non-iterables cannot be destructured as arrays.

Iterables are only iterated until all bindings are assigned.

The rest binding is eagerly evaluated and creates a new array, instead of using the old iterable.

Object destructuring

Basic assignment, assigning to new variable names.

A property can be unpacked from an object and assigned to a variable with a different name than the object property.

Here, for example, const { p: foo } = o takes from the object o the property named p and assigns it to a local variable named foo .

Assigning to new variable names and providing default values

A property can be both

  • Unpacked from an object and assigned to a variable with a different name.
  • Assigned a default value in case the unpacked value is undefined .

Unpacking properties from objects passed as a function parameter

Objects passed into function parameters can also be unpacked into variables, which may then be accessed within the function body. As for object assignment, the destructuring syntax allows for the new variable to have the same name or a different name than the original property, and to assign default values for the case when the original object does not define the property.

Consider this object, which contains information about a user.

Here we show how to unpack a property of the passed object into a variable with the same name. The parameter value { id } indicates that the id property of the object passed to the function should be unpacked into a variable with the same name, which can then be used within the function.

You can define the name of the unpacked variable. Here we unpack the property named displayName , and rename it to dname for use within the function body.

Nested objects can also be unpacked. The example below shows the property fullname.firstName being unpacked into a variable called name .

Setting a function parameter's default value

Default values can be specified using = , and will be used as variable values if a specified property does not exist in the passed object.

Below we show a function where the default size is 'big' , default co-ordinates are x: 0, y: 0 and default radius is 25.

In the function signature for drawChart above, the destructured left-hand side has a default value of an empty object = {} .

You could have also written the function without that default. However, if you leave out that default value, the function will look for at least one argument to be supplied when invoked, whereas in its current form, you can call drawChart() without supplying any parameters. Otherwise, you need to at least supply an empty object literal.

For more information, see Default parameters > Destructured parameter with default value assignment .

Nested object and array destructuring

For of iteration and destructuring, computed object property names and destructuring.

Computed property names, like on object literals , can be used with destructuring.

Invalid JavaScript identifier as a property name

Destructuring can be used with property names that are not valid JavaScript identifiers by providing an alternative identifier that is valid.

Destructuring primitive values

Object destructuring is almost equivalent to property accessing . This means if you try to destruct a primitive value, the value will get wrapped into the corresponding wrapper object and the property is accessed on the wrapper object.

Same as accessing properties, destructuring null or undefined throws a TypeError .

This happens even when the pattern is empty.

Combined array and object destructuring

Array and object destructuring can be combined. Say you want the third element in the array props below, and then you want the name property in the object, you can do the following:

The prototype chain is looked up when the object is deconstructed

When deconstructing an object, if a property is not accessed in itself, it will continue to look up along the prototype chain.

Specifications

Browser compatibility.

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

  • Assignment operators
  • ES6 in Depth: Destructuring on hacks.mozilla.org (2015)

Next: Increment Operators , Previous: Boolean Expressions , Up: Expressions   [ Contents ][ Index ]

8.6 Assignment Expressions ¶

An assignment is an expression that stores a new value into a variable. For example, the following expression assigns the value 1 to the variable z :

After this expression is executed, the variable z has the value 1. Whatever old value z had before the assignment is forgotten. The ‘ = ’ sign is called an assignment operator .

Assignments can store string values also. For example, the following expression would store the value "this food is good" in the variable message :

(This also illustrates concatenation of strings.)

Most operators (addition, concatenation, and so on) have no effect except to compute a value. If you ignore the value, you might as well not use the operator. An assignment operator is different. It does produce a value, but even if you ignore the value, the assignment still makes itself felt through the alteration of the variable. We call this a side effect .

The left-hand operand of an assignment need not be a variable (see Variables ). It can also be an element of a matrix (see Index Expressions ) or a list of return values (see Calling Functions ). These are all called lvalues , which means they can appear on the left-hand side of an assignment operator. The right-hand operand may be any expression. It produces the new value which the assignment stores in the specified variable, matrix element, or list of return values.

It is important to note that variables do not have permanent types. The type of a variable is simply the type of whatever value it happens to hold at the moment. In the following program fragment, the variable foo has a numeric value at first, and a string value later on:

When the second assignment gives foo a string value, the fact that it previously had a numeric value is forgotten.

Assignment of a scalar to an indexed matrix sets all of the elements that are referenced by the indices to the scalar value. For example, if a is a matrix with at least two columns,

sets all the elements in the second column of a to 5.

When an assignment sets the value of a vector, matrix, or array element at a position or dimension outside of that variable’s current size, the array size will be increased to accommodate the new values:

Attempting to increase the size of an array such that the desired output size is ambiguous will result in an error:

This is because adding the 9th element creates an ambiguity in the desired array position for the value 10, each possibility requiring a different array size expansion to accommodate the assignment.

Assignments may be made with fewer specified elements than would be required to fill the newly expanded array as long as the assignment is unambiguous. In these cases the array will be automatically padded with null values:

For all built-in types, the null value will be appropriate to that object type.

Numeric arrays:

Logical arrays:

Character arrays:

Cell arrays:

Struct arrays:

Note that Octave currently is unable to concatenate arbitrary object types into arrays. Such behavior must be explicitly defined within the object class or attempts at concatenation will result in an error. See Object Oriented Programming

Assigning an empty matrix ‘ [] ’ works in most cases to allow you to delete rows or columns of matrices and vectors. See Empty Matrices . For example, given a 4 by 5 matrix A , the assignment

deletes the third row of A , and the assignment

deletes the first, third, and fifth columns.

Deleting part of an array object will necessarily resize the object. When the deletion allows for consistent size reduction across a dimension, e.g., one element of a vector, or one row or column of a matrix, the size along that dimension will be reduced while preserving dimensionality. If, however, dimensionality cannot be maintained, the object will be reshaped into a vector following column-wise element ordering:

An assignment is an expression, so it has a value. Thus, z = 1 as an expression has the value 1. One consequence of this is that you can write multiple assignments together:

stores the value 0 in all three variables. It does this because the value of z = 0 , which is 0, is stored into y , and then the value of y = z = 0 , which is 0, is stored into x .

This is also true of assignments to lists of values, so the following is a valid expression

that is exactly equivalent to

In expressions like this, the number of values in each part of the expression need not match. For example, the expression

is equivalent to

The number of values on the left side of the expression can, however, not exceed the number of values on the right side. For example, the following will produce an error.

The symbol ~ may be used as a placeholder in the list of lvalues, indicating that the corresponding return value should be ignored and not stored anywhere:

This is cleaner and more memory efficient than using a dummy variable. The nargout value for the right-hand side expression is not affected. If the assignment is used as an expression, the return value is a comma-separated list with the ignored values dropped.

A very common programming pattern is to increment an existing variable with a given value, like this

This can be written in a clearer and more condensed form using the += operator

Similar operators also exist for subtraction ( -= ), multiplication ( *= ), and division ( /= ). An expression of the form

is evaluated as

where op can be either + , - , * , or / , as long as expr2 is a simple expression with no side effects. If expr2 also contains an assignment operator, then this expression is evaluated as

where temp is a placeholder temporary value storing the computed result of evaluating expr2 . So, the expression

You can use an assignment anywhere an expression is called for. For example, it is valid to write x != (y = 1) to set y to 1 and then test whether x equals 1. But this style tends to make programs hard to read. Except in a one-shot program, you should rewrite it to get rid of such nesting of assignments. This is never very hard.

Back to Savannah Homepage

  • Not Logged in
  • Clean Reload
  • Area to search in Groups People Support Bugs Tasks Patches  
  • Hosted Projects
  • Hosting requirements
  • Register New Project
  • Contributors Wanted
  • User Docs: FAQ
  • User Docs: In Depth Guide
  • Get Support
  • Contact Savannah
  • GNU Project
  • All GNU Packages
  • Dev Resources
  • License List
  • GNU Mirrors

Support freedom

  • Free Software Foundation
  • Coming Events
  • Free Software Directory
  • Cryptographic software legal notice
  • Copyright infringement notification
  • Related Forges
  • Savannah Non-GNU

GNU Octave - Bugs: bug #32067, about error "value on right...

  • View members
  • Mailing lists
  • Browse Web Pages Repository
  • Use Mercurial
  • Browse Mercurial repository
  • Reset to open

Dependencies

  • Get statistics

bug #32067 : about error "value on right hand side of assignment is undefined"

Post a comment.

value on right hand side of assignment is undefined

Attached Files

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:         Comment:    

No files currently attached

Depends on the following items: None found

Items that depend on this one: None found

Mail Notification Carbon-Copy List

There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

Only group members can vote.

Please enter the title of George Orwell 's famous dystopian book (it's a date):

Follow 2 latest changes.

Back to the top

Copyright © 2024 Free Software Foundation, Inc. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. The Levitating, Meditating, Flute-playing Gnu logo is a GNU GPL'ed image provided by the Nevrax Design Team. Page source code

Powered by Savane 3.13-67b0. Corresponding source code

Re: "value on right hand side of assignment is undefined"

  • Re: "value on right hand side of assignment is undefined" , Scott R. Kuindersma   <=
  • Prev by Date: Re: gset use in octave-2.9.3
  • Next by Date: panic segment fault error
  • Previous by thread: "value on right hand side of assignment is undefined"
  • Next by thread: How do I just load (read) a file? (beginner's question)

error: Value on right hand side of assignment is undefined

CONFIDENTIAL: The information contained in this email (including any attachments) is confidential, subject to copyright and for the use of the intended recipient only. If you are not the intended recipient please delete this message after notifying the sender. Unauthorised retention, alteration or distribution of this email is forbidden and may be actionable. Attachments are opened at your own risk and you are advised to scan incoming email for viruses before opening any attached files. We give no guarantee that any communication is virus-free and accept no responsibility for virus contamination or other system loss or damage of any kind.

  • Re: error: Value on right hand side of assignment is undefined , Doug Stewart , 2018/11/23
  • Re: error: Value on right hand side of assignment is undefined , mmuetzel , 2018/11/23
  • error: value on right hand side of assignment is undefined , Mishal0488 , 2018/11/23
  • Prev by Date: error: value on right hand side of assignment is undefined
  • Next by Date: error: value on right hand side of assignment is undefined
  • Previous by thread: error: value on right hand side of assignment is undefined
  • Next by thread: Re: error: Value on right hand side of assignment is undefined

COMMENTS

  1. Problem with my code?

    I get the following message that I don't understand: 'error: value on right hand side of assignment is undefined'. 0 Comments. Show -2 older comments Hide -2 older comments. Sign in to comment. Sign in to answer this question. Answers (2) Star Strider on 18 Oct 2019. Vote. 0. Link.

  2. Octave: error: value on right hand side of assignment is undefined

    Octave: error: value on right hand side of assignment is undefined #2436. Open andrescodas opened this issue May 17, 2019 · 0 comments Open ... SXFunction a = 3 b = 3 error: value on right hand side of assignment is undefined. The text was updated successfully, but these errors were encountered:

  3. Re: error: value on right hand side of assignment is undefined

    > getting the following error: "value on right hand side of assignment is > undefined". The script and function code are shown below respectively. > > input = [ 0 0 1;0 1 1;1 0 1;1 1 1]; ... value on right hand side of assignment is undefined, Ian McCallion <= Re: error: ...

  4. error: value on right hand side of assignment is undefined

    [Prev in Thread] Current Thread [Next in Thread] error: value on right hand side of assignment is undefined, Mishal0488, 2018/11/23. error: Value on right hand side of assignment is undefined, Mishal.Mohanlal, 2018/11/23. Re: error: Value on right hand side of assignment is undefined, Doug Stewart, 2018/11/23; Re: error: Value on right hand side of assignment is undefined, mmuetzel, 2018/11/23

  5. Nullish coalescing operator (??)

    The nullish coalescing operator can be seen as a special case of the logical OR ( ||) operator. The latter returns the right-hand side operand if the left operand is any falsy value, not only null or undefined. In other words, if you use || to provide some default value to another variable foo, you may encounter unexpected behaviors if you ...

  6. ReferenceError: Invalid left-hand side in assignment

    Finally we arrived to actual problem: ("rock" && two) can't be evaluated to l-value that can be assigned to (in this particular case it will be value of two as truthy). Note that if you'd use braces to match perceived priority surrounding each "equality" with braces you get no errors.

  7. error: value on right hand side of assignment is undefined

    Re: error: value on right hand side of assignment is undefined, Ian McCallion, 2020/08/07. Re: error: value on right hand side of assignment is undefined, Brett Green, 2020/08/07. Re: error: value on right hand side of assignment is undefined, andetroy, 2020/08/07; Re: error: value on right hand side of assignment is undefined, andetroy, 2020/08/07

  8. Re: error: value on right hand side of assignment is undefined

    error: value on right hand side of assignment is undefined, andetroy, 2020/08/07. Re: error: value on right hand side of assignment is undefined, Ian McCallion, 2020/08/07. Re: error: value on right hand side of assignment is undefined, Brett Green, 2020/08/07. Re: error: value on right hand side of assignment is undefined, andetroy <=

  9. SyntaxError: invalid assignment left-hand side

    Invalid assignments don't always produce syntax errors. Sometimes the syntax is almost correct, but at runtime, the left hand side expression evaluates to a value instead of a reference, so the assignment is still invalid. Such errors occur later in execution, when the statement is actually executed. js. function foo() { return { a: 1 }; } foo ...

  10. javascript

    That's the way the language was designed. It is consistent with most languages. Having a variable declaration return anything other than undefined is meaningless, because you can't ever use the var keyword in an expression context.. Having assignment be an expression not a statement is useful when you want to set many variable to the same value at once:. x = y = z = 2;

  11. Destructuring assignment

    In an array destructuring from an array of length N specified on the right-hand side of the assignment, if the number of variables specified on the left-hand side of the assignment is greater than N, only the first N variables are assigned values. The values of the remaining variables will be undefined.

  12. Re: [Error] value on right hand side of assignment is undefined

    breakinpoint wrote > Hi, i'm looking for some help. > I created 2 functions (trab1 and alinea2) both on octave root. > > trab1 code: > > alinea2 code: > > The problem is that when call alinea2 i get "value on right hand side of > assignment is undefined" at line:17 column:6 and i can't solve it. > If anyone can help me with this i would appreciate. A first trick is to specify in the Octave ...

  13. Assignment Ops (GNU Octave (version 9.1.0))

    The left-hand operand of an assignment need not be a variable (see Variables). It can also be an element of a matrix (see Index Expressions) or a list of return values (see Calling Functions). These are all called lvalues, which means they can appear on the left-hand side of an assignment operator. The right-hand operand may be any expression.

  14. GNU Octave

    The following lines of code each return the error: "value on right hand side of assignment is undefined". a='a';b= {};c=cellfun (@ (x)strcmp (a,x),b) c=get (findobj (0,'Tag','xxx'),'a') while MATLAB returns c= [] on both. Octave 3.2.4 also returns [] for the first example. Guillaume <gyom>.

  15. Re: "value on right hand side of assignment is undefined"

    "value on right hand side of assignment is undefined", 广来 邓, 2005/06/13 Prev by Date: Re: gset use in octave-2.9.3 Next by Date: panic segment fault error

  16. error: Value on right hand side of assignment is undefined

    error: Value on right hand side of assignment is undefined. Date: Fri, 23 Nov 2018 13:48:33 +0200. Hi Guys. Please refer to the below code. This is simplified case of what I am using, just to demonstrate my issue. Once complied, the functions are available to be used in the command window. "Function B" however does not work, as it gives the ...

  17. Re: error: Value on right hand side of assignment is undefined

    Subject: Re: error: Value on right hand side of assignment is undefined. Date: Fri, 23 Nov 2018 09:27:47 -0600 (CST) mmuetzel wrote. > Your function A doesn't have a return value. Also you should use a. > variable. > that has the same name as the function it is in.

  18. error: Value on right hand side of assignment is undefined

    Your function A doesn't have a return value. Also you should use a variable. that has the same name as the function it is in. Even if it isn't a syntax. error, it might be confusing. Try something like: function val = A (x, y) val = x + y; endfunction.

  19. Re: error: Value on right hand side of assignment is undefined

    Subject: Re: error: Value on right hand side of assignment is undefined. Date: Fri, 23 Nov 2018 10:12:20 -0500. On Fri, Nov 23, 2018 at 9:44 AM < address@hidden > wrote: What I see is that you have a function called B and a variable called B. This is not good!

  20. javascript

    That's can cause errors. "// The left-hand side should return foo2" - it doesn't return a variable, it returns the value of the logical expression. Expression works on the left side, (foo1 && foo2 && foo3) returns undefined which makes the left-hand side operator invalid for assignment.

  21. error: value on right hand side of assignment is undefined

    error: value on right hand side of assignment is undefined, Williams, Timothy J Mr CECOM RDEC NVESD <= Prev by Date: Re: Matrices Next by Date: octave and vim

  22. typescript

    The left-hand side of an assignment expression may not be an optional property access.ts(2779) I have created custom pipe where if I get loop through text and search for a word, if word is present in given text I add highlighted class to it so that, I can hilight that word with pink color