How to use Matlab Functions with Vector of Structures

1 回表示 (過去 30 日間)
Giovanni Bambini
Giovanni Bambini 2023 年 2 月 2 日
コメント済み: Giovanni Bambini 2023 年 2 月 2 日
I have code:
for i=1:n
struct(i).field1 = rand();
end
I would like to do:
m = min(struct.field1); % same with min(struct(:).field1)
but Matlab keeps giving me "Error: too many arguments."
I tried to convert to array, cells, tables, etc. every Matlab function keeps giving me that error, and it's like I cannot work with vectorized structures.
Can someone help?
E.
  4 件のコメント
Stephen23
Stephen23 2023 年 2 月 2 日
編集済み: Stephen23 2023 年 2 月 2 日
"And how am I supposed to know that that thing is called "comma separated list""
Simple answer:
I do not assume that you know that term, which is exactly why I gave you a link to the documentation and my tutorial.
Complete answer:
By using the MATLAB documentation.
You are already using a structure, so lets search (famous internet search engine) for "MATLAB structure" and see what we get. The very first result is this (probably you get something similar when you search "structure" in the MATLAB help):
After syntax and basic examples about halfway down the page it states "When you access a field of a nonscalar structure, such as s.f, MATLAB® returns a comma-separated list...", but ufortunately without highlighting or linking this term. However at the bottom of the page we can always find some useful links under the "See Also" and "Topics" lists, so lets try there are well. The complete list of Topics is currently:
Hmm... well we don't have nested structures, nor are we generating fieldnames, and cell arrays are not relevant either... so lets click the first link. We skim that page (it is not very long nor very complex) and see at the end the section "Related Topics", which contains:
Hmm, that first link looks interesting... lets open it. Indeed once we open it, it describes and shows things very much like what you are already doing. That seems promising. Oh, written just a few lines down the page: "MATLAB® returns the data from the elements in a comma-separated list...", this time emphasized to catch our eye.
And once again, at the bottom of the page we can find another list of links:
The first one seems like a good link to click on, if I want to learn more about comma-separated lists.
Summary: learn to use the MATLAB help. If the links at the bottom don't seem useful, probably browsing the content heirarchy will get you somewhere. The more you practice using it, the better you will understand how it is arranged by topic and product.
Giovanni Bambini
Giovanni Bambini 2023 年 2 月 2 日
Thanks for the detailed answer

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

採用された回答

Matt J
Matt J 2023 年 2 月 2 日
m = min([struct.field1])

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by