Home How to return the leaves of a struct as vector in Matlab?
Reply: 0

How to return the leaves of a struct as vector in Matlab?

user638
1#
user638 Published in May 25, 2018, 6:27 pm

Often I need to access the leaves of data in a structured array for calculations. How is this best done in Matlab 2017b?

% Minimal working example:
egg(1).weight = 30;
egg(2).weight = 33;
egg(3).weight = 34;

someeggs = mean([egg.weight]) % works fine

apple(1).properties.weight = 300;
apple(2).properties.weight = 330;
apple(3).properties.weight = 340;

someapples = mean([apple.properties.weight]) %fails
weights = [apple.properties.weight] %fails too

% Expected one output from a curly brace or dot indexing expression, 
% but there were 3 results.
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30918 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO