How to use Matlab Functions with Vector of Structures
2 views (last 30 days)
Show older comments
Giovanni Bambini
on 2 Feb 2023
Commented: Giovanni Bambini
on 2 Feb 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 Comments
Stephen23
on 2 Feb 2023
Edited: Stephen23
on 2 Feb 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:
- Structure Arrays
- Access Data in Nested Structures
- Generate Field Names from Variables
- Cell vs. Structure Arrays
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:
- Access Elements of a Nonscalar Structure Array
- Generate Field Names from Variables
- Create Cell Array
- etc.etc.
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.
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!