How to add elements to a list

2,121 views (last 30 days)
Winston DeGraw
Winston DeGraw on 15 Jun 2017
Commented: Walter Roberson on 16 Jun 2017
I've tried several different options for adding elements to a list, examples:
ls=[]
ls=[ls,element];
Which served only to concatenate the elements that I added, with the same issue happening here:
ls=append(ls,element);
Any other options?
  1 Comment
Stephen23
Stephen23 on 16 Jun 2017
Edited: Stephen23 on 16 Jun 2017
  1. MATLAB does not have any "list" data type. The common MATLAB data types are arrays: numeric, cell, struct, etc.
  2. [] is an array concatenation operator, not a list operator, as the documentation clearly describes.
  3. Adding an element to an array can be achieved using indexing or concatenation. What behavior do you expect that you are not seeing?
The best place to learn about very basic MATLAB concepts, such as what data types there are and how to access them, is to do the introductory tutorials (which are highly recommended for all beginners):

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 Jun 2017
ls(end+1) = element;
"Which served only to concatenate the elements that I added"
What behavior are you hoping for that you are not seeing?
  2 Comments
Stephen23
Stephen23 on 16 Jun 2017
Surely this has the same effect as the code shown in the question?
Walter Roberson
Walter Roberson on 16 Jun 2017
They asked "Any other options?"

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!