Accessing cell array contents

1 回表示 (過去 30 日間)
Dave O
Dave O 2022 年 6 月 30 日
コメント済み: Rik 2022 年 6 月 30 日
I have a one-dimensional cell array where each element of the array contains a timetable with identical format. Is there a direct way of creating a vector that contains the last element of one specific variable in each timetable (without looping)?
For example, something like:
CellArray{:}.Variable(end)
  2 件のコメント
Dave O
Dave O 2022 年 6 月 30 日
編集済み: Dave O 2022 年 6 月 30 日
Thanks for your reply, Rik and Adam!
Rik
Rik 2022 年 6 月 30 日
You're welcome. If either answer solved your problem, feel free to mark it as accepted answer. If the other was helpful as well, consider giving it an upvote.

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

採用された回答

Rik
Rik 2022 年 6 月 30 日
編集済み: Rik 2022 年 6 月 30 日
No. You could use cellfun, but that will only hide the loop. Using a loop directly tends to have better efficiency.
But don't worry: loops are not as bad as you might think (especially if you pre-allocate the output). They are only bad when there isn't a builtin equivalent that operates on the entire array.
  1 件のコメント
Adam Danz
Adam Danz 2022 年 6 月 30 日
+1 Rik, @Dave O if a loop is more readable to you than cellfun, go with the loop.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2022 年 6 月 30 日
In this example, the cell array is named myCellArray and the variable is named myVar.
y = cellfun(@(A)A.myVar(end), myCellArray)
If the values you are extracting are non-numeric or non-scalar,
y = cellfun(@(A){A.myVar(end)}, myCellArray)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by