필터 지우기
필터 지우기

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

조회 수: 8 (최근 30일)
Christian
Christian 2020년 6월 18일
댓글: Adam Danz 2020년 6월 19일
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