Prateek Raj Gautam
Prateek Raj Gautam

Reputation: 27

Get properties from multilevel objects in matlab in matrix form

I want to get values from a multilevel object property in the form of a matrix here in below example

clear all
close all
% dummy initialization of myStruct holding desired property 'x' 
for i=1:10
    myStruct(i).userdata.error.x=i;% just to illustrate
end

%% working method
Userdata=[myStruct.userdata];
Error=[Userdata.error];
X=[Error.x]%this is required matrix of all x's

is there any better alternative to get it in one step

Upvotes: 0

Views: 39

Answers (1)

Wolfie
Wolfie

Reputation: 30046

You could use arrayfun, but this is basically just a loop in disguise to reverse your dummy example:

X = arrayfun( @(i) myStruct(i).userdata.error.x, 1:numel(myStruct) );

Upvotes: 2

Related Questions