identifying and removing a double within a structure

I'm trying to produce a mean of a column within a structure. the problem is that within this column one of the rows is a double 1X2.
Another problem is that the next step would be to get this mean for each participant (each participant produces a structure). Some of the participants may not have a double within or may have more than one, or have several doubles with different sizes.
When there is a double, I want to get only the last input number and ignore the rest.
I tried several solutions but eventually I got stuck.
Can someone put me in the right direction on how to identify a double within a structure and removing parts of it?
Thanks in advance!!!

8 Comments

so do you have an N dimensional struct with a field which contains the double(s) ?
yes, exactly - checking for size returns 18 1 10 (10 is the number of variables and 18 is rows).
checking for class returns 'struct'.
Checking for size and class of the variable I wanted, returns an error
Essentially no chance we're going to be able to figure out the details without a sample of the struct array you're trying to deal with. Attach a .mat file that illustrates the problem -- with the answer(s) expected and how arrived at.
thanks for replying! I attached the file I'm using.
The variable I want is EXPDATA.trials.responses_results
this is how it looks like:
And the answer I expect is 0.889.
I tried to struct2table and struct2cell, cell2mat ext, all resulted in errors eventually.
I was thinking the best approach would be a for loop where 'i' would be the row - if value is double - take the end value of double, else pull the value of row i in struct. Not sure how to pull this off though.
maybe you thought about something like that.
myStruct(1).myDoubleField=[1 2];
myStruct(2).myDoubleField=[1 2 3];
myStruct(3).myDoubleField=[1 3 8];
myStruct(4).myDoubleField=[4];
lastVals=zeros(numel(myStruct),1);
for nr=1:numel(myStruct)
lastVals(nr)=myStruct(nr).myDoubleField(end);
end
meanResult=mean(lastVals)
another possibility with cellfun
allFields={myStruct.myDoubleField};
lastVal =@(in) in(end);
mean(cellfun(lastVal,allFields))
yes! worked perfectly, thank you!
great. i move it down to the answers section

Sign in to comment.

Answers (1)

Jonas
Jonas on 3 Jul 2021
Edited: Jonas on 3 Jul 2021
maybe you thought about something like that.
myStruct(1).myDoubleField=[1 2];
myStruct(2).myDoubleField=[1 2 3];
myStruct(3).myDoubleField=[1 3 8];
myStruct(4).myDoubleField=[4];
lastVals=zeros(numel(myStruct),1);
for nr=1:numel(myStruct)
lastVals(nr)=myStruct(nr).myDoubleField(end);
end
meanResult=mean(lastVals)
another possibility with cellfun
allFields={myStruct.myDoubleField};
lastVal =@(in) in(end);
mean(cellfun(lastVal,allFields))

Categories

Products

Release

R2020b

Asked:

on 2 Jul 2021

Edited:

on 3 Jul 2021

Community Treasure Hunt

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

Start Hunting!