MATLAB Answers

Accessing elements of lists within lists

67 views (last 30 days)
I have created a list which contains several lists in the following code.
a = [{'poh',3,4},{'v',5}]
a=[{'poh',3,4},{'v',5}]
a =
1×5 cell array
{'poh'} {[3]} {[4]} {'v'} {[5]}
Matlab interprets the elements as cells, but they should be treated as strings or integers/floats. Also, I want a(1) to equal {'poh',3,4} not 'poh' so elements within the sublist can be accessed by referencing it, for example, a(1)(2) = 3. Does anyone have suggestions regarding how this can be done?
  2 Comments
Stephen Cobeldick
Stephen Cobeldick on 9 Oct 2020
"I have created a list which contains several lists ..."
MATLAB does not have "list" class. What you show is one single cell array formed by concatenating two cell arrays together (because the square brackets [] are a concatenation operator, not a "list" operator (which does not exist)).

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 9 Oct 2020
In Matlab cell arrays are the only type that can store heterogeneous data.
Just change the outside [] to {} if you want to have nested cell arrays.
a={{'poh',3,4},{'v',5}}
a{1}{2}
  2 Comments
Mohammad Sami
Mohammad Sami on 9 Oct 2020
Yes that will work, assuming the data is of the same type.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!