简体   繁体   English

Matlab:如何合并结构数组字段的值

[英]Matlab: How to merge the values of struct array fields

Is it possible to easily combine values from struct array fields into a single array without looping through each individual struct in the array? 是否可以轻松地将结构数组字段中的值组合到单个数组中,而无需遍历数组中的每个单独的结构?

For reference, see the attached code: 供参考,请参见随附的代码:

% build random struct array with only one field
% for demonstration only
clear i s out;
for i = 1:10
    s(i).value = rand;
end
s

% not working, as it returns multiple results
s(1:end).value

% combine all "value" into a single array using for-loop
out = zeros(length(s), 1);
for i = 1:length(s)
    out(i) = s(i).value;
end
out

Simply put, the goal is to kind of "merge" all "value" fields. 简而言之,目标是要“合并”所有“值”字段。

You can get it using the following: 您可以使用以下方法获取它:

out = [s.value]

The s.value returns all the values and [...] to make an array of them. s.value返回所有的价值观和[...]使他们的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM