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

5 ビュー (過去 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 Center および File ExchangePreprocessing Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by