varfun applied to timetable, how can i keep only 1 of several outputs?

5 vues (au cours des 30 derniers jours)
Yves Haag
Yves Haag le 25 Nov 2020
Réponse apportée : dpb le 25 Nov 2020
Hi!
I want to calculate a slope of all columns in a timetable. I apply:
fslope = @(x) polyfit(linspace(1,size(x,1),size(x,1)),x,1) % Slope
like this:
varfun(fslope, X)
what i get is a new timetable with the correct # of columns and rows. However, as polyfit(x,y,1) returns 2 value, those output timetable columns contain two 2 values, which are now grouped. How can i remove one of those (keep only the slope, remove the constant of my polyfit)?
Thanks a lot!

Réponse acceptée

dpb
dpb le 25 Nov 2020
Two ways I see:
Write function that wraps polyfit and only returns the slope instead of using anonymous function. Presuming the above code is already in an m-file script or function this wouldn't be too bad to make a local function within existing file.
Alternatively, just fix up the existing table/timetable -- I happened to have a table already in workspace so
tfit=varfun(@(y) polyfit([1:numel(y)].',y,1),t,'InputVariables',t.Properties.VariableNames(2:end));
tfit=array2table(tfit{1,:}(1:2:end),'VariableNames',tfit.Properties.VariableNames);
Just write over the original table with the retrieved slopes array. The end result of above here was:
>> tfit
tfit =
1×4 table
Fun_P1 Fun_P2 Fun_V Fun_S
______ _______ _____ _______
10 0.59253 1.315 0.11128
>>
With your timetable, use it instead of course. Not sure what you did about the time; don't recall what varfun does with the time in a timetable by default, actually...

Plus de réponses (0)

Catégories

En savoir plus sur Preprocessing Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by