How to pass variables to function used for varfun

8 Ansichten (letzte 30 Tage)
asaad sellmann
asaad sellmann am 9 Feb. 2022
Beantwortet: Chetan am 10 Nov. 2023
I have a table with several variables to describe the acceleration data in that table. So in every row I know which patient did what exercise and in what set and which repetition and wich arm(side) they used.
{'patient'} {'side'} {'exercise'} {'set'} {'repetition'}
{'s0_ACC_x'}
{'s0_ACC_y'} {'s0_ACC_z'} {'s0_GYRO_x'} {'s0_GYRO_y'}
{'s0_GYRO_z'} {'s0_MAG_x'} {'s0_MAG_y'} {'s0_MAG_z'}
{'s1_ACC_x'} {'s1_ACC_y'} {'s1_ACC_z'} {'s1_GYRO_x'}
{'s1_GYRO_y'} {'s1_GYRO_z'} {'s1_MAG_x'} {'s1_MAG_y'}
{'s1_MAG_z'} {'s2_ACC_x'} {'s2_ACC_y'} {'s2_ACC_z'}
{'s2_GYRO_x'} {'s2_GYRO_y'} {'s2_GYRO_z'} {'s2_MAG_x'}
{'s2_MAG_y'} {'s2_MAG_z'}
Now I am trying to find the repetition of each set that is the longest (by entries in the table...groupcount), then interpolate all the variables vartype(double) of the remaining repetitions of that set to the length of the longest. I need that to then average all 5 repetitions per datapoint. Sort of normalising to the longest repetition.
I tried the following:
function [varargout] = longestRepetition(dataMRNFILTER)
replength = groupcounts(dataMRNFILTER,[{'patient'}, {'exercise'}, {'side'}, {'set'}, {'repetition'}]);
GroupVars = {'patient', 'exercise', 'side', 'set'};
maxreplength = rowfun(@max, replength,...
"GroupingVariables", GroupVars,...
"InputVariables", 'GroupCount', 'OutputVariableNames', {'max', 'LongestRepetition'});
varargout{1} = maxreplength;
varargout{2} = replength;
end
and then:
function [varargout] = stretchReps(replength, maxreplength, dataMRNFILTER)
GroupVars = {'patient', 'exercise', 'side', 'set'};
InputVars = dataMRNFILTER(:,vartype('double')).Properties.VariableNames;
counterBegin = 1;
counterEnd = 0;
counterindex = 1;
for j=1:height(maxreplength)
for i=1:5
counterEnd = counterEnd + replength.GroupCount(counterindex);
dataRep{i} = dataMRNFILTER(counterBegin:counterEnd,:);
counterBegin = counterEnd+1;
counterindex = counterindex+1;
end
longestRep = maxreplength.LongestRepetition(j);
lenlongestRep = maxreplength.max(j)+1;
for k=1:5
func = @(x) interp1(1:height(x), x,1:height(x)/lenlongestRep:height(x));
lengthUnifiedReps{j}.rep{k} = varfun(func,...
dataRep{k},...
"GroupingVariables", GroupVars, "InputVariables", InputVars);
lengthUnifiedReps{j}.rep{k}.Properties.VariableNames=regexprep(lengthUnifiedReps{j}.rep{k}.Properties.VariableNames, 'Fun_', ''); % loeschen des "FUN_" im Variablennamen
lengthUnifiedReps{j}.rep{k} = removevars(lengthUnifiedReps{j}.rep{k}, 'GroupCount');
end
end
varargout{1} = lengthUnifiedReps;
end
But at this point I am stuck.... the output of lengthUnifiedReps{j}.rep{k} looks something like this:
I honestly have no idea how to transpose those entries and why they were transposed and why did it kill all the other entries for the first 4 variables?! They are the same, but still this kills my table :D
So I was hoping to rewrite this using varfun and a more fancy function, but my skills for that seem to be to low... so any help on nested funcitons or on my code would be highly appreciated!!
thanks a lot!

Antworten (1)

Chetan
Chetan am 10 Nov. 2023
I understand that you're trying to find the longest repetition for each set and then adjust all the variables of the remaining repetitions to match the length of the longest one. This seem to be having an issue where the data is transposed when you apply the 'varfun' function.
Here's a potential solution for this:
1. Define an interpolation function to apply to each group:
interpFunc = @(x) interp1(1:size(x, 1), x, linspace(1, size(x, 1), max(size(x, 1))));
2. Group the data by patient, exercise, side, and set:
groupVars = {'patient', 'exercise', 'side', 'set'};
groupedData = groupsummary(dataMRNFILTER, groupVars);
3. Apply the interpolation function to each group:
interpData = varfun(interpFunc, groupedData, 'GroupingVariables', groupVars, 'InputVariables', vartype('double'));
4. Remove the 'GroupCount' variable from the output:
interpData = removevars(interpData, 'GroupCount');
This approach should interpolate each variable in the table to the length of the longest repetition for each set, grouped by patient, exercise, side, and set.
I hope this helps!

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by