当前位置:网站首页>Matlab 2012a 绘制带箭头的线段
Matlab 2012a 绘制带箭头的线段
2022-06-30 01:52:00 【Intimes】
Matlab 2012a 自带的line函数可以绘制线段,但无法绘制箭头。
此外还有一个函数叫 annotation,可以绘制箭头线段,但遗憾的是,只能在0到1的范围内绘制,和可能是出于什么特殊的考虑吧。
因此,需要自制函数来绘制带箭头的线段。在网上找了个代码,但是在Matlab 2012a环境下运行有问题,因此进行了一些改动。可以运行了。
首先来看下效果:

效果还是比较满意的,可以绘制漂亮的箭头线段,且范围不用限制在0到1的范围内,当然,运行效率等方面可以还是可以改进的。
下面直接上代码”
clc;clear;
x1 = [1 2];
y1 = [5 7];
h1 = drawarrow(x1,y1);
hold on;
axis([0 5 0 7]);
x2 = [3 1];
y2 = [3 5];
h2 = drawarrow(x2,y2);这是测试用的程序,下面是函数
function h = drawarrow(x,y,lineType,ax)
% https://blog.csdn.net/qq_41093957/article/details/115256114
% 这个程序最大的问题是 改变了plot的范围 从而使线段位置不对了
switch nargin
case 2
lineType='arrow';
ax=gca;
case 3
ax=gca;
end
% 调整坐标大小以适应箭头长度
% xlimraw=get(ax,'XLim');
% ylimraw=get(ax,'YLim');
xlim=get(ax,'XLim');
ylim=get(ax,'YLim');
xlimmin=xlim(1);xlimmax=xlim(2);
ylimmin=ylim(1);ylimmax=ylim(2);
if xlimmin>min(x(1),y(1)), xlimmin=min(x(1),y(1));end
if xlimmax<max(x(1),y(1)), xlimmax=max(x(1),y(1));end
if ylimmin>min(x(2),y(2)), ylimmin=min(x(2),y(2));end
if ylimmax<max(x(2),y(2)), ylimmax=max(x(2),y(2));end
% ax.XLim = [xlimmin,xlimmax];
% ax.YLim = [ylimmin,ylimmax];
set(ax,'XLim',[xlimmin,xlimmax]);
set(ax,'YLim',[ylimmin,ylimmax]);
xlim=get(ax,'XLim');
ylim=get(ax,'YLim');
% xlim=ax.XLim;
% ylim=ax.YLim;
pos=get(ax,'Position');
x_ratio = pos(3)/(xlim(2)-xlim(1));
y_ratio = pos(4)/(ylim(2)-ylim(1)); % 缩放比例
orig_pos=[-xlim(1)*x_ratio+pos(1),-ylim(1)*y_ratio+pos(2)]; % figure坐标系中的原点坐标
x=x.*[x_ratio,y_ratio];y=y.*[x_ratio,y_ratio];
x=x+orig_pos;y=y+orig_pos;
h = annotation(lineType,[x(1),y(1)],[x(2),y(2)]);
% set(ax,'XLim',xlimraw);
% set(ax,'YLim',ylimraw);
end
这里要鸣谢一下原作者。他的程序是没有问题的,只是可能不适应2012a的环境,改动之后,就可以用了。
边栏推荐
- Yyds dry inventory consistent and smooth zoom type and spacing
- Cookie encryption 13
- The national industrial information security development research center issued the report on industrial information security situation in 2021
- Want to change careers, but don't know what you want to do?
- C language irony
- 013_ slider
- [machine learning Q & A] accuracy, accuracy, recall, ROC and AUC
- Conversion between opencv and image (valid for pro test)
- Que se passe - t - il si un faux divorce devient un vrai divorce?
- js返回内容被unicode编码
猜你喜欢

C语言 一元多项式求导

JS prototype and prototype chain (Lantern Festival meal)

DTW学习(dynamic time warping)——思想、代码实现

Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED

什么是幂等性?四种接口幂等性方案详解!

Write this number in C

Cookie encryption 12

一次 Keepalived 高可用的事故,让我重学了一遍它!

cookie加密11

【机器学习Q&A】数据抽样和模型验证方法、超参数调优以及过拟合和欠拟合问题
随机推荐
Cookie encryption 8
[mrctf2020]ezpop-1 | PHP serialization
C语言 写出这个数
002_ container
一次 Keepalived 高可用的事故,让我重学了一遍它!
cookie加密8
012_ switch
005_ button
GeoTools WKT坐标系转换
Derivation of univariate polynomial in C language
C语言 害死人不偿命的(3n+1)猜想
想转行,但不知道自己要做什么工作比较好?
Geotools wkt coordinate system conversion
【机器学习Q&A】数据抽样和模型验证方法、超参数调优以及过拟合和欠拟合问题
ES6 one line code for array de duplication
Fake divorce turns into real divorce. What about property
C语言 一元多项式求导
城市规划馆在设计制作上需要注意什么
Summary of DOM
MySQL monitoring 3