フィルターのクリア

Accessing elements of lists within lists

36 ビュー (過去 30 日間)
Aleem Andrew
Aleem Andrew 2020 年 10 月 8 日
コメント済み: Stephen23 2020 年 10 月 9 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 10 月 9 日
Matlab interprets the elements as cells, but they should be treated as strings or integers/floats.
You are being confused by the display format that MATLAB uses to emphasize that you are dealing with a cell.
1×5 cell array
{'poh'} {[3]} {[4]} {'v'} {[5]}
is saying that a(1) is {'pho'}, which is correct: when you use () indexing on a cell array, you get back a cell array rather than the contents.
There were a lot of releases in the past where it would have presented the output as
'pho' [3] [4] 'v' [5]
which is just a choice of formatting.
The display is not telling you that a{1} is {'pho'} but rather that a(1) is {'pho'}
Stephen23
Stephen23 2020 年 10 月 9 日
"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)).

サインインしてコメントする。

採用された回答

Mohammad Sami
Mohammad Sami 2020 年 10 月 9 日
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 件のコメント
Aleem Andrew
Aleem Andrew 2020 年 10 月 9 日
編集済み: Aleem Andrew 2020 年 10 月 9 日
Thanks for your answer Is there a way to perform operations on the elements or compare their values, for example, to add element 2 to element 3 by typing something like a{1}{2}+a{1}{3} which should output the integer/double 7? And a{1}{1} == '0' should equal false?
Mohammad Sami
Mohammad Sami 2020 年 10 月 9 日
Yes that will work, assuming the data is of the same type.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by