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

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

It looks like the coordinate values are limited by precision.
See this answer for an explanation and solution.

7 Comments

This resolves the issue. Thank you!
One more question... It does work properly on only one plot. What do I have to change for other plots in the same script?
Edit: nvm my question, I solved it by fetching the current figure handle.
fig = get(groot,'CurrentFigure');
dcm = datacursormode(fig);
set(dcm, 'UpdateFcn', @customDataCursorUpdateFcn, 'Enable', 'On');
Instead of
fig = get(groot,'CurrentFigure');
you could use
fig = gcf();
or better yet, use the figure handle directly,
fig = figure();
Using gcf() or get(groot,'CurrentFigure') could result in unintended consequences. If the user clicks on a different figure at the wrong time, the changes will be applied to the wrong figure.
Don't mind what I wrote here. I was dumb and forgot to add the path for your custom function... lol
I used the solution right below. Thanks Sir!
fig = figure();
Does not work. For some reason it doesn't fetch the correct parameters and data tips won't work at all.
fig = gcf();
Does also not work. My R2020a release doesn't know that function. Also the documentation of gfc states this:
To get the handle of the current figure without forcing the creation of a figure if one does not exist, query the CurrentFigure property on the root object.
fig = get(groot,'CurrentFigure');
MATLAB® returns fig as an empty array if there is no current figure.
Although it will most probablz not trouble the user as the programm only plots 2 figure, I'd like to know how I can optimize this further. So please let me know if I am doing something wrong while trying your suggestions. Thank you!
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');
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 Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 18 Jun 2020

Commented:

on 19 Jun 2020

Community Treasure Hunt

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

Start Hunting!