- MATLAB does not have any "list" data type. The common MATLAB data types are arrays: numeric, cell, struct, etc.
- [] is an array concatenation operator, not a list operator, as the documentation clearly describes.
- Adding an element to an array can be achieved using indexing or concatenation. What behavior do you expect that you are not seeing?
How to add elements to a list
1,231 views (last 30 days)
Show older comments
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
on 16 Jun 2017
Edited: Stephen23
on 16 Jun 2017
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):
Accepted Answer
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
More Answers (1)
Korosh Agha Mohammad Ghasemi
on 25 Jun 2024
Moved: Voss
on 25 Jun 2024
To efficiently add elements to a list (or array) in MATLAB without causing concatenation issues, you should use cell arrays or preallocate memory for your array if you know the number of elements in advance. Here are some different methods to add elements to a list in MATLAB:
Using Cell Arrays
If you don't know the number of elements in advance or if the elements are of different types, using a cell array is a good approach:
ls = {}; % Initialize an empty cell array
element = 'newElement'; % Example element
% Adding elements to the cell array
ls{end+1} = element;
% Display the cell array
disp(ls);
Preallocating Memory
If you know the number of elements you will be adding, preallocate memory to avoid resizing the array each time you add an element:
n = 10; % Assume you want to add 10 elements
ls = zeros(1, n); % Preallocate an array of zeros with size 1x10
for i = 1:n
ls(i) = i; % Assign values to the array
end
% Display the array
disp(ls);
Using Dynamic Arrays
If you need to dynamically grow your array without preallocation, you can use the end+1 indexing for numeric arrays:
ls = []; % Initialize an empty array
element = 5; % Example element
% Adding elements to the array
ls(end+1) = element;
% Display the array
disp(ls);
Using append Function with Strings
If you're working with strings and need to append them, use the strcat or append function:
str = ""; % Initialize an empty string
element = "newElement"; % Example element
% Append the element to the string
str = append(str, element);
% Display the string
disp(str);
Example Combining Different Elements
Here’s a combined example that dynamically adds elements to a cell array:
ls = {}; % Initialize an empty cell array
% Elements to be added
elements = {1, 'text', [1, 2, 3], {4, 5}, 6.7};
% Adding elements to the cell array
for i = 1:length(elements)
ls{end+1} = elements{i};
end
% Display the cell array
disp(ls);
Each of these methods allows you to add elements to a list (or array) in MATLAB without running into concatenation issues. Choose the method that best fits your specific needs.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!