|
| 1 | +function [v,d,xy] = applyfun(SW,fun,A) |
| 2 | + |
| 3 | +%APPLYFUN Apply function to extract values along swath |
| 4 | +% |
| 5 | +% Syntax |
| 6 | +% |
| 7 | +% [v,d] = applyfun(SW,fun) |
| 8 | +% [v,d] = applyfun(SW,fun,A) |
| 9 | +% |
| 10 | +% Description |
| 11 | +% |
| 12 | +% This functions applies a function fun to summarize/aggregate values |
| 13 | +% along the swath SW. fun must be a function that takes a 2D-matrix and |
| 14 | +% computes values along the first dimension of an array (e.g. @mean). |
| 15 | +% fun can also be a cell array of functions. |
| 16 | +% |
| 17 | +% Input arguments |
| 18 | +% |
| 19 | +% SW SWATHobj |
| 20 | +% fun function or character or cell array of function |
| 21 | +% A GRIDobj |
| 22 | +% |
| 23 | +% Output arguments |
| 24 | +% |
| 25 | +% v values mapped to trace |
| 26 | +% d distance along trace |
| 27 | +% xy trace coordinates |
| 28 | +% |
| 29 | +% Example |
| 30 | +% |
| 31 | +% DEM = GRIDobj('srtm_bigtujunga30m_utm11.tif'); |
| 32 | +% x = [382209 389559]'; |
| 33 | +% y = [3790773 3799443]'; |
| 34 | +% SW = SWATHobj(DEM,x,y,'width',1000); |
| 35 | +% [v,d] = applyfun(SW,@(x) quantile(x,[.1:.1:.9])); |
| 36 | +% plot(d,v) |
| 37 | +% |
| 38 | +% See also: SWATHobj, SWATHobj/plotdz |
| 39 | +% |
| 40 | +% Author: Wolfgang Schwanghart (schwangh[at]uni-potsdam.de) |
| 41 | +% Date: 27. March, 2026 |
| 42 | + |
| 43 | +arguments |
| 44 | + SW SWATHobj |
| 45 | + fun |
| 46 | + A = [] |
| 47 | +end |
| 48 | + |
| 49 | +if ~isempty(A) |
| 50 | + SW = mapswath(SW,A); |
| 51 | +end |
| 52 | + |
| 53 | +% Place function in cell array |
| 54 | +if ~iscell(fun) |
| 55 | + fun = {fun}; |
| 56 | +end |
| 57 | + |
| 58 | +% Iterate through cell array of functions |
| 59 | +for r = 1:numel(fun) |
| 60 | + % Convert string or character array to anonymous function |
| 61 | + if ischar(fun{r}) || isstring(fun{r}) |
| 62 | + f = str2func(fun{r}); |
| 63 | + else |
| 64 | + f = fun{r}; |
| 65 | + end |
| 66 | + |
| 67 | + if r == 1 |
| 68 | + v = f(SW.Z); |
| 69 | + else |
| 70 | + v = vertcat(v,f(SW.Z)); %#ok<AGROW> |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +v = v'; |
| 75 | + |
| 76 | +if size(v,1) ~= size(SW.Z,2) |
| 77 | + error('TopoToolbox:error',... |
| 78 | + 'Function output inconsistent with size of trace.') |
| 79 | +end |
| 80 | +d = SW.distx; |
| 81 | +xy = SW.xy; |
| 82 | + |
| 83 | + |
0 commit comments