How to use Matlab Functions with Vector of Structures

1 vue (au cours des 30 derniers jours)
Giovanni Bambini
Giovanni Bambini le 2 Fév 2023
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 commentaires
Stephen23
Stephen23 le 2 Fév 2023
Modifié(e) : Stephen23 le 2 Fév 2023
"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 le 2 Fév 2023
Thanks for the detailed answer

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 2 Fév 2023
m = min([struct.field1])

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by