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

조회 수: 6 (최근 30일)
Yves Haag
Yves Haag 2020년 11월 25일
답변: dpb 2020년 11월 25일
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!

채택된 답변

dpb
dpb 2020년 11월 25일
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...

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Preprocessing Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by