How to use Matlab Functions with Vector of Structures

2 次查看(过去 30 天)
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.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2023-2-2
m = min([struct.field1])

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by