List comprehension-like function in assigning values in nested structures

133 ビュー (過去 30 日間)
Sam
Sam 2019 年 10 月 9 日
回答済み: meghannmarie 2019 年 10 月 9 日
Hi everyone,
I am coming from writing in Python and am very used to thinking in list comprehensions to form arrays/lists.
Specifically, in MATLAB I'd like to have a more comprehensive version of the following code, especially if the length of the data structure gets larger.
Suppose I have a data structure:
data(1).h = 5;
data(2).h = 4;
Now, I'd like to have all 'h' elements in one array like this:
h(1:2) = [data(1).h data(2).h]
As you see, I am now writing out the entire list, which will get very unwieldy when dealing with structures that go up to data(i) with i very large.
Does anyone have a suggestion how to do this "MATLAB-style", without using a for-loop?
Thanks in advance.

採用された回答

meghannmarie
meghannmarie 2019 年 10 月 9 日
Try this:
h = [data.h];

その他の回答 (1 件)

Daniel M
Daniel M 2019 年 10 月 9 日
You'll find the answer in this article on comma-separated lists, specifically with the deal() function. comma-separated lists

カテゴリ

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