cheatsheet for dealing with structures?
显示 更早的评论
One thing that I find hardest in matlab is dealing with structures and extracting information from then. For me it seems there is not enough clear examples how to extract data in different situations. Is there any cheatsheet made for this? whether its data, [data], {data}, [data].', data{i} etc, there should be examples for all of these situations....
here is example; im trying to get all timestamps "timestamp" (for finding duplicate fields) of sub stucture "trainLocations" from every first cell of main structure "trainTraffic". Everything I have tried has been in vain...
trainTraffic(1).trainLocations(1).timestamp == {trainTraffic.trainLocations(1).timestamp}.'
error is : Intermediate dot '.' indexing produced a comma-separated list with 57 values, but it must produce a single value when followed by subsequent indexing operations.

2 个评论
"For me it seems there is not enough clear examples how to extract data in different situations. Is there any cheatsheet made for this?"
Perhaps this:
and in the official documentation:
"whether its data, [data], {data}, [data].', data{i} etc, there should be examples for all of these situations...."
None of those have anything (directly) to do with structures. Those are various syntactic-sugar operators and some totally unrelated indexing and transpose operations. Just like every other function or operator MATLAB (or any other programming language for that matter) does not exhaustively list out every single example [], [A], [A,B], [A,B,C], etc. for every single version of A, B, C, etc which can be variables, expressions, function calls, comma-separated lists, etc. Nor should documentation that explains the concatenation operator explain what the transpose operator does... just as the documentation that explains comma-separated lists should not explain how to use every other operator and function.
Consider how many permutations of operators your proposed documentation would have...
Your proposed documentation would be ... truly enormous. It would be difficult to find anything in it.
"im trying to get all timestamps "timestamp" (for finding duplicate fields) of sub stucture "trainLocations" from every first cell of main structure "trainTraffic""
TRAINTRAFFIC is a structure, it does not have cells.
"Intermediate dot '.' indexing produced a comma-separated list with 57 values, but it must produce a single value when followed by subsequent indexing operations."
That error message is explained in detail here:
采纳的回答
更多回答(1 个)
Understanding MATLAB Data Containers
Structures (struct): A structure is a data type in MATLAB that allows for the storage of data of different types and sizes. Structures are accessed in two primary ways:
- Field Access: Use dot notation to access fields within a structure, e.g., struct.fieldName.
- Element Access: For an array of structures, access individual structures using parentheses, e.g., structArray(index).
Cell Arrays ({}): Cell arrays are containers that can hold data of varying types and sizes. There are two main ways to interact with cell arrays:
- Content Access: Use curly braces to access the contents of individual cells, e.g., cellArray{index} retrieves the content of the cell at index.
- Element Access: Use parentheses to access the cell itself as a unit, e.g., cellArray(index) refers to the cell at index without extracting its contents.
Arrays ([]): Arrays are collections of elements of the same type. MATLAB supports various types of arrays, such as numeric arrays, character arrays, and logical arrays.
- Element Access: Use parentheses to access individual elements of an array, e.g., array(index).
Example Problem
Given a structure array trainTraffic with a substructure trainLocations containing a field timestamp, and you want to get all timestamp values from the first trainLocations of each trainTraffic entry.
% Number of elements in trainTraffic
numElements = numel(trainTraffic);
% Preallocate a cell array for timestamps
timestamps = cell(numElements, 1);
% Loop through each element of trainTraffic
for i = 1:numElements
% Access the first trainLocations of each trainTraffic element and its timestamp
timestamps{i} = trainTraffic(i).trainLocations(1).timestamp;
end
% timestamps cell array now contains the desired timestamp values
Explanation
- numel(trainTraffic): Counts how many elements are in trainTraffic.
- Preallocate a cell array timestamps: It's good practice for efficiency and clarity.
- Loop through each trainTraffic: Extract the timestamp from the first trainLocations and store it in the timestamps cell array.
Additional Points:
- Nesting: You can nest these data containers within each other for more complex data structures.
- Specific Methods: Each container type might have additional methods for specific operations, such as adding, removing, or iterating through elements. Refer to the MATLAB documentation for details.
References
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
2 个评论
"Access elements with dot notation, e.g., struct.field."
No, this is incorrect.
Fields are accessed using dot indexing.
Elements of a structure are accessed using parentheses (just like every other array class in MATLAB).
"Access elements with curly braces, e.g., cellArray{index}."
Again, incorrect.
The cell content is accessed using curly-braces.
Elements of a cell array are accessed using parentheses (just like every other array class in MATLAB).
Hassaan
2024-2-21
Thanks for your insight. Updated.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!