plot() shows multiple y values for the same x value

13 次查看(过去 30 天)
Hi,
I imported a very long one row vector of sampling data to matlab. There are some outliers which I like to manually delete. Therefore I plotted the sampling data and searched for the outliers. But it turned out that the data tip doesn't show the exact x value for the outlier. In fact it does show a number of y values for the same x value as in the screenshot. I tried several methods for the x vector but before I realized that the displayed x value is wrong. See my code below. SampleData is a 1x1365252 double vector, so naturally N is 1365252.
Is there a way to get the correct postition without zooming in all the way? It'll be a huge hustle to do that for every outlier.
% Import sampling data
delimiterIn = ' ';
headerlinesIn = 0;
SampleData=transpose(importdata('Messelektronik2020_150er_64OSR_230400Baud_ISR_Sample_2.log', delimiterIn, headerlinesIn));
% determine how many samples there are in total
N=length(SampleData);
% generate x vector
x=linspace(0,N-1,N);
% plot
figure (1);
plot(x, SampleData);

采纳的回答

Adam Danz
Adam Danz 2020-6-18
It looks like the coordinate values are limited by precision.
See this answer for an explanation and solution.
  7 个评论
Adam Danz
Adam Danz 2020-6-19
编辑:Adam Danz 2020-6-19
The first command,
fig = figure();
is called when the figure is generated, not when you're trying to get the handle to an existing figure. This is, by far, the best solution.
The second command,
fig = gcf();
does the same exact thing as get(groot,'CurrentFigure'). In fact, if you type "help gcf" you'll see that,
The handle of the current figure is stored in the root property CurrentFigure ...
The gcf function was introduced before 2006 and it absolutely exists in the r2020a release. The only difference between gcf and get(groot,'CrrentFigure') is that if a figure doesn't exist, the prior will create one and the latter will not, as you found in the documentation. If you had problems with gcf other than that please described them and I'd be happy to help fix that. This function is used quite often and if it doesn't work on your end, it should be fixed.
But to reiterate, the first suggestion above is, by far, the best approach and you'll see that recommendation all throughout this forum. Both the gcf and the get(...) functions are susceptible to errors in cases where an unexpected figure is current. It is always recommended to use the parent handle instead of gcf, gca, gco, etc.
Demo:
fig = figure(1);
plot(fig, x, SampleData);
dcm = datacursormode(fig);
set(dcm, 'UpdateFcn', @customDataCursorUpdateFcn, 'Enable', 'On');
Adam Danz
Adam Danz 2020-6-19
Just saw your updated comment. -- no prob.
Not to beat a dead horse but the best practice is to get the figure handle directly from the figure-creation using
fig = figure(. . .);
Same with axes
ax = axes(. . .)
or any graphics object
h = plot(. . .);
This is the 'Best Practice". It's fool-proof. Getting into the habit of storing and using parent handles appropriately will greatly improve your code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by