metro
metro

Reputation: 13

Matlab: How can I call object properties using a string?

I am currently working on a data analysis program that contains two objects: Experiment and RunSummary. The Experiment object contains multiple instances of the RunSummary object. Each RunSummary object contains multiple properties (row matrices) each containing different data points for a given run.

For example: Experiment.RunSummary(5).Tmean is row matrix containing all of the average torque values for run 5 in my experiment.

I am currently trying to find a way to combine selected common properties from specific runs into a single matrix that can be used for further analysis. The current way I have had to do this is:

X(:,1) = [Drilling.Runs(1).Tmean,...
             Drilling.Runs(2).Tmean,...
                 Drilling.Runs(3).Tmean,...
                     Drilling.Runs(5).Tmean]';

X(:,2) = [Drilling.Runs(1).Fmean,...
             Drilling.Runs(2).Fmean,...
                 Drilling.Runs(3).Fmean,...
                     Drilling.Runs(5).Fmean]';

This code takes the average torque (Tmean) and average force (Fmean) from runs 1, 2, 3, and 5 and combines them in a single matrix, X, with Tmean for all runs in the first column and Fmean in the second. Although this method works, I have over 20 different properties and 15 different runs making this coding very tedious.

I have tried using code such as get(Experiment.RunSummary(i),'Tmean') to try and retrieve these property matricies, but was met with the error:

Conversion to double from RunSummary is not possible.

Thanks, metro

Edit: Drilling is the name of the Experiment object. Runs is the name of the RunSummary object.

Upvotes: 1

Views: 293

Answers (1)

TroyHaskin
TroyHaskin

Reputation: 8401

You can use dynamic fields. The documentation is for structs, but the same principal works for classes (at least on my R2012a install). You can also use the comma-separate nature of object array indexing to compress the code. Example:

I      = [1,2,3,5]              ;
props  = {'Tmean','Fmean'}      ;
Nprops = length(props)          ;
X      = zeros(length(I),Nprops);

for k = 1:Nprops 
    X(:,k) = [Drilling.Runs(I).(props{k})]';
end

Upvotes: 3

Related Questions