如何在这个MATLAB中找到Annotate extends ...吗?

时间:2016-10-16 21:19:42

标签: matlab annotations matlab-figure

我现在输出中有最小值和最大值(图1),但是我希望得到排序最大值的标签(图2)(最高得1,......)和最小值(最低得1) 。 我可以通过以下方式完成图1的输出,但我无法将这些注释集成到函数

var md = new Remarkable({
    html:true,
    langPrefix:'lang-',
    highlight: function (str, lang) {
    alert('highlighting'); // never executes!
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(lang, str).value;
      } catch (err) {}
    }
    try {
      return hljs.highlightAuto(str).value;
    } catch (err) {}

    return ''; // use external default escaping
  }
});

var test = md.render('<code class="lang-js">var x = 1;</code>');

只是将close all; clear all; clc; % https://se.mathworks.com/help/signal/ref/findpeaks.html % http://stackoverflow.com/a/26837689/54964 x = linspace(0,1,1000); Pos = [1 2 3 5 7 8]/10; Hgt = [4 4 2 2 2 3]; Wdt = [3 8 4 3 4 6]/100; for n = 1:length(Pos) Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2); end PeakSig = sum(Gauss) - exp(sum(Gauss))/10; plot(x, PeakSig); hold on; [p l]=findpeaks(PeakSig); %,x); %,'Annotate','extents','WidthReference','halfheight') plot(x(l), p, 'ko', 'MarkerFaceColor', 'g'); [pn ln]=findpeaks(-PeakSig); %,x); %,'Annotate','extents','WidthReference','halfheight') plot(x(ln), -pn, 'ko', 'MarkerFaceColor', 'r'); title('Signal Peak Widths') 附加到'Annotate','extents','WidthReference','halfheight')并不会在应用程序等中工作,显然是因为前一行[p l]=findpeaks(...)无法理解单行中所带来的额外内容相应的变量

plot(x(l), p, 'ko', 'MarkerFaceColor', 'g');

图。 1没有这些注释的当前输出, 图2预期输出但带有最大值和最小值的注释

enter image description here enter image description here

MATLAB:2016b
操作系统:Debian 8.5 64位
硬件:华硕Zenbook UX303UA

2 个答案:

答案 0 :(得分:1)

以下是达到此目的的一种方法:

x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 2 2 2 3];
Wdt = [3 8 4 3 4 6]/100;
Gauss = zeros(numel(Pos),numel(x));
for n = 1:numel(Pos)
    Gauss(n,:) =  Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss) - exp(sum(Gauss))/10;

% get the peaks:
[p,xmax] = findpeaks(PeakSig,x);
maxsrt = sortrows([xmax;p].',-2);
[pn,xmin] = findpeaks(-PeakSig,x);
minsrt = sortrows([xmin;-pn].',2);

% plotting:
blue = [0 0.447 0.741];
plot(x,PeakSig,xmax,p+0.2,'v','MarkerFaceColor',blue,'MarkerEdgeColor',blue);
% you can comment the line above and uncomment the line below, if you
% prefer:
% findpeaks(PeakSig,x,'Annotate','peaks');
text(maxsrt(:,1),maxsrt(:,2)+0.2,num2str((1:numel(p)).'),'FontSize',14,...
    'VerticalAlignment','bottom','HorizontalAlignment','center')
ylim([-10 3]);
grid on
hold on
plot(xmin,-pn-0.2,'^','MarkerFaceColor',blue,'MarkerEdgeColor',blue);
hold off
text(minsrt(:,1),minsrt(:,2)-0.2,num2str((1:numel(pn)).'),'FontSize',14,...
    'VerticalAlignment','top','HorizontalAlignment','center')
title('Signal Peak Widths')

peaks

答案 1 :(得分:1)

这是使用TypeError: adverts(...) is undefined执行此操作的另一种方法。您可以将所有位置(xy)和注释类型连接到一个数组(gscatter下方),而不是两次调用plot(或者更多,如果需要其他注释),并将它们全部绘制在一起通过pks的类型(即组):

gscatter

结果恰好是same,但这更短,也许更优雅。