Access multiple structure fields and put them in a new structure or vector

8 views (last 30 days)
I have a cell structure with below layout. In below example I acces a single field.
dataArray{1,(2)}(1,1)
Is there a way to acces multiple fields and get them into a new structure or a vector without using a for loop?
E.g. dataArray{1,(2:10)}(1,1)
I'm afraid I use the brackets in a wrong way.

Accepted Answer

Stephen23
Stephen23 on 15 Feb 2021
Edited: Stephen23 on 15 Feb 2021
No, what you are asking for is not possible. This syntax:
dataArray{1,2:10}
creates a comma-separated list of separate arrays, which cannot be further indexed into using one pair of parentheses as your example shows.
You can use parentheses (as opposed to curly braces) to return that part of the cell array:
sub = dataArray(1,2:10)
You could use that together with cellfun to return the first element of each of those cells:
out = cellfun(@(a)a(1,1),dataArray(1,2:10))

More Answers (0)

Community Treasure Hunt

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

Start Hunting!