Clear Filters
Clear Filters

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

8 views (last 30 days)
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);

Accepted Answer

Adam Danz
Adam Danz on 18 Jun 2020
It looks like the coordinate values are limited by precision.
See this answer for an explanation and solution.
  7 Comments
Adam Danz
Adam Danz on 19 Jun 2020
Edited: Adam Danz on 19 Jun 2020
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 on 19 Jun 2020
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!