首页 > 解决方案 > 从阶梯图 Matlab 获取数据向量

问题描述

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

h = stairs(x,y);

在此处输入图像描述

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

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

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

任何帮助表示赞赏。谢谢!

在此处输入图像描述

标签: matlabmatlab-figure

解决方案


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

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

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

代码示例:

%% 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') ;

在此处输入图像描述


推荐阅读