从阶梯图Matlab获取数据向量

Get data vector from stairstep plot Matlab

如何从 Matlab 中的 stairs 函数的输出中获取数据向量?我尝试了以下

h = stairs(x,y);

然后我从句柄中获取数据:

x = h.XData; 
y = h.YData; 

但是绘制 x 和 y 时,它们看起来是分段函数,而不是阶梯函数。

感谢任何帮助。 谢谢!

Matlab 文档明确指出:

[xb,yb] = stairs(___) does not create a plot, but returns matrices xb and yb of the same size, such that plot(xb,yb) plots the stairstep graph.

显示stairs图所需的数据相对容易自己生成。

假设您有 xy。要生成 2 个向量 xsys,例如 plot(xs,ys) 将显示与 stairs(x,y) 相同的内容,您可以使用以下两步方法:

  • 复制xy
  • 的每个元素
  • 将新向量偏移一个元素(删除一个向量的第一个点和另一个向量的最后一个点)

代码示例:

%% demo data
x = (0:20).';
y = [(0:10),(9:-1:0)].' ;

hs = stairs(x,y) ;
hold on

%% generate `xs` and `ys`
% replicate each element of `x` and `y` vector
xs = reshape([x(:) x(:)].',[],1) ;
ys = reshape([y(:) y(:)].',[],1) ;

% offset the 2 vectors by one element
%  => remove first `xs` and last `ys`
xs(1)   = [] ;
ys(end) = [] ;

% you're good to go, this will plot the same thing than stairs(x,y)
hp = plot(xs,ys) ;

% and they will also work with the `fill` function
hf = fill(xs,ys,'g') ;