How do I extract data from a structure and put them all in one single array?

10 次查看(过去 30 天)
I have a 1X31 struct with two fields, I would like to extract all the data from the second one, and put them all in one single array.
How can I do that?
Here are some images to illustrate how they are arranged.
So what I want to do is to create an array that contains dataTT(1).Data + dataTT(2).Data + ... dataTT(n).Data

采纳的回答

Stephen23
Stephen23 2023-4-12
  2 个评论
Stephen23
Stephen23 2023-4-12
编辑:Stephen23 2023-4-12
I guess your next request will be to convert that string data into numeric... Here are some approaches:
S = ["{1.23,4.56,7.89}";"{9.87,6.54,3.21}";"{12.3,45.6,78.9}";"{98.7,65.4,32.1}"]
S = 4×1 string array
"{1.23,4.56,7.89}" "{9.87,6.54,3.21}" "{12.3,45.6,78.9}" "{98.7,65.4,32.1}"
M = sscanf([S{:}],'{%f,%f,%f}',[3,Inf]).'
M = 4×3
1.2300 4.5600 7.8900 9.8700 6.5400 3.2100 12.3000 45.6000 78.9000 98.7000 65.4000 32.1000
M = double(split(replace(S,["{","}"],''),','))
M = 4×3
1.2300 4.5600 7.8900 9.8700 6.5400 3.2100 12.3000 45.6000 78.9000 98.7000 65.4000 32.1000

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2023-4-12
编辑:Image Analyst 2023-4-12
You forgot to attach your data in a .mat file with the paperclip icon so we can't try it with your data. You can try these things:
v = {dataTT.Topic}
or
v = vertcat(dataTT.Topic)
Same for the Data field
  2 个评论
Image Analyst
Image Analyst 2023-4-12
You're welcome. Just additional info to extend this into future situations you might encounter: If the numbers were single numbers, not cell arrays of 3 numbers then you could do
v = [structName.fieldName] % Concatenate all fields into a single vector.
For a real world example, where mask is a binary image with several "blobs" that you want to measure the area of:
props = regionprops(mask, 'Area'); % Returns a structure with "Area" as a field of the structure.
allAreas = [props.Area]; % Concatenate all areas into a single double vector.
I use it that way all the time.
If my replies helped you, perhaps you could click the "Vote" icon. 🙂

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by