How to access specific data when using "getfield"

2 views (last 30 days)
Hello all,
I am using the following code to analyse some data stored in a structure:
for i=1:10;
mean_value (i) = mean(getfield(Charge,strcat('Curve',int2str(i))));
"Charge" is the name of the structure, and "Curve" is each one of the 10 array of data inside it. So for this example, I get a vector with 10 elements that shows the average value of each group of data (the structure contains 10x(8670x1) elements). Now I want to take only the first 1416 elements of each array and to get its mean value. But I don't know how to do this.
Any suggestion? Thanks you so much!
Best regards.

Answers (3)

Kye Taylor
Kye Taylor on 27 Jun 2012
If I understand your question, this may work...
for i=1:10;
myData(:,i) = getfield(Charge,strcat('Curve',int2str(i)));
mean_value = mean(myData);
meanValueFirstBunch = mean(myData(1:1416,:));

Leah on 27 Jun 2012
You can reference the field of a structure dynamically with Charge.Curve1 or Charge.('Curve1') Loren has some nice blog posts on structures
for fname=fieldnames(Charge)'
  1 Comment
Jan on 27 Jun 2012
fname{1} is more efficient than char(fname).

Sign in to comment.

Walter Roberson
Walter Roberson on 27 Jun 2012
for i = 1 : 10 mean_value(i) = mean(Charge.(str2num(i, 'Curve%d'))); end

Community Treasure Hunt

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

Start Hunting!