当前位置:网站首页>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的环境,改动之后,就可以用了。
边栏推荐
- C语言 数组元素循环右移问题
- Varnish foundation overview 4
- Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)
- (1) Basic learning - figure out the difference between pin, pad, port, IO and net
- Geotools wkt coordinate system conversion
- A summary of the quantification of deep network model
- Varnish foundation overview 6
- Fake divorce turns into real divorce. What about property
- How to deal with occasional bugs?
- C语言 说反话
猜你喜欢
![[mrctf2020]ezpop-1 | PHP serialization](/img/65/9b7a3ae3552d8784b0c77a1130d762.png)
[mrctf2020]ezpop-1 | PHP serialization

C语言 成绩排名

ES6 one line code for array de duplication

cookie加密11

C语言 害死人不偿命的(3n+1)猜想
![[machine learning Q & A] data sampling and model verification methods, hyperparametric optimization, over fitting and under fitting problems](/img/ec/de75eb8279604a57ef7d90406ad7c7.png)
[machine learning Q & A] data sampling and model verification methods, hyperparametric optimization, over fitting and under fitting problems

Kubernetes core object overview details

Cookie加密15 登录加密

Cookie encryption 11

Want to change careers, but don't know what you want to do?
随机推荐
C language continues (3n+1) conjecture
C语言 写出这个数
假离婚变成真离婚,财产怎么办
Cookie加密10
[machine learning Q & A] data sampling and model verification methods, hyperparametric optimization, over fitting and under fitting problems
Varnish foundation overview 7
js内容混淆,返回内容加密
MySQL monitoring 3
Understand AQS principle (flow chart and synchronous queue diagram)
Difference between test plan and test plan
Varnish foundation overview 1
GeoTools:WKT、GeoJson、Feature、FeatureCollection相互转换常用工具
Yyds dry inventory consistent and smooth zoom type and spacing
Varnish foundation overview 10
[binary tree] maximum binary tree II
208. implement trie (prefix tree) - attach detailed notes
If mybaits cannot query the data, it can query how to change it in the database
App test related tools
C语言 一元多项式求导
Conversion between opencv and image (valid for pro test)