matlab - 从阶梯图 Matlab 获取数据向量
问题描述
如何从 Matlab 中楼梯函数的输出中获取数据向量?我尝试了以下
h = stairs(x,y);
然后我从句柄中获取数据:
x = h.XData;
y = h.YData;
但是当绘制 x 和 y 时,它们看起来是分段函数,而不是楼梯。
任何帮助表示赞赏。谢谢!
解决方案
显示stairs
绘图所需的数据相对容易由您自己生成。
假设你有x
和y
。要生成 2 个向量xs
,ys
例如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') ;
推荐阅读
- php - 正则表达式 - preg_replace - 一个返回空格,另一个不返回任何内容
- android - 在 Windows 10 上没有 android studio 环境的 React Native
- reactjs - React-Table Hooks - 更改页面时重置 pageIndex
- javascript - 通过检查 Class .active 来增加进度条的宽度
- python - python 3中的Json获取元素
- tcp - 为什么 TCP-BSD 服务器即使收到数据也会卡在 read() 中?
- typescript - 当测试明显失败时,vscode-jest 插件显示成功
- python - 如何计算嵌套列表或元组中的重叠值以打印出 =
- mysql - MySQL 5.6 中的 any_type 是否有任何替代方案
- reactjs - 我们可以使用 key 来更新链接到具有相同 key 的另一个组件的组件吗