0

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.

1 Answer 1

2

You can get it using the following:

out = [s.value]

The s.value returns all the values and [...] to make an array of them.

Sign up to request clarification or add additional context in comments.

1 Comment

I was absolutely sure that I did try this, but obviously I was wrong. Thanks! For completeness: The (:) is not needed, only the [...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.