MATLAB Data Cursor won't click on every point of plot (R2018b) - matlab

I need to get the plot values in between those two points, but when I click with the data cursor in between, the label appears either left or right, as if the cursor only picks up points on a fixed grid.
Is there a way to increase this precision so I can click and get the values in between?

Found the solution:
In the code, this needs to be set after the plot:
dcmObj = datacursormode;
set(dcmObj,'SnapToDataVertex','off')

Related

Matlab second set of axes not the same dimensions as the first

What I really want to do is what this question is all about:
matlab remove only top and right ticks with leaving box on
To paraphrase the original question, I want to have a plot with tick marks on the left and bottom axes but NO ticks on the top and right axes, but with the box outlining the plot still intact.
Since I don't have enough reputation, I wasn't able to simply comment to follow up. Anyway, I execute the same code given in the answer by mc2, and what I get is the figure below. The logic of the method is to use 2 axes that have the same dimensions and overlay them on top of each other. The first axis has the 'box' property turned off, the second one does not. The code is:
a = gca;
set(a,'box','off','color','none')
b = axes('Position',get(a,'Position'),'box','on','xtick',[],'ytick',[]);
axes(a)
Assume that the axis that represents "a" already exists. That's the initial figure. (I added the blacked out part on top of the screen shot.) The second axis that was created is clearly not the same dimensions as the initial one--it's the bigger, nearly-square box that surrounds the smaller, rectangular one. Instead, I want the big, nearly-square box to be same dimensions as the smaller one.
Somehow the get(a,'Position') function is not grabbing the correct dimensions that I want it to. Why is this happening?
I didn't make the original figure myself; a collaborator generated the data and sent me *.fig file.

MatLab GUI plotting a line that updates with button press

So I'm working on MatLab GUI assignment right now.
It's basically an estimation game. In every trial the user guesses the correlation displayed on the left axes. When they click submit my code calculates the absolute value of the difference between their estimation and the actual correlation.
So far so good.
On the right axes I want to plot a line that updates every time they click "submit". The x-coordinate would be the trial # and the y-coordinate would be the absolute difference previously mentioned.
I can plot this information successfully using points instead of a line by using "scatter" or "plot", but when I try to make it a line, nothing appears, although the axis does seem to update...
Both of the following codes work if the marker is '.' or 'o' or 's' or 'x' ... literally any marker.. but I can't get it to connect the dots... I've messed around with trying to use animated line and drawnow but that didn't work out for me either..
plot(handles.trial, handles.diff(handles.trial),'-.'); hold on; %plot trialwise absolute differences
or
scatter(handles.trial, handles.diff(handles.trial),'-.'); hold on; %plot trialwise absolute differences
**Problem solved!
see the solution below
Solution:
Store all of the relevant data in a matrix handles.DATA. For this to work there needed to be something in there before the first trial so during initialization I set handles.DATA = [0 0] and then with the button-press (submit) that ends a trial, the data from that trial got concatenated into the data matrix: handles.DATA = vertcat(handles.DATA, [x y]). Below this I could do the plot that I wanted to: plot(handles.DATA(:,1), handles.DATA(:,2),'Color','r'
*note: don't hold on because then you will have lines stacking on each other.
**also if you don't specify line color, the line will become a different color every time you click "submit" because this is generating a new line every time based off the updated information.

Matlab: datacursormode and calculate slope

I'm trying to calculate the slope between two points. I've tried some other methods such as imline, but none of them allows the snapping to a curve. I found out this morning that I can possibly use the data tips in the datacursormode.
So, what I want to do is:
Select two points showing the black dot for both points
Upon the second left-clicking, a line is drawn connecting the two points and a text box shows the calculated slope
If I move either black dot, the text box is updated with a new slope
I can have as many pairs as I want, many lines between a part of black dots
If either dot is deleted, the associated text box disappears along with it
I've tried things like
waitforbuttonpress;
point1 = getCursorInfo(data_cursor_mode_obj);
waitforbuttonpress;
point2 = getCursorInfo(data_cursor_mode_obj);
slope = abs((y2-y1)/(x2-x1));
text((x1+x2)/2,(y1+y2)/2,[' \leftarrow' num2str(slope) ])
This of course doesn't update the text. In fact, for some reason, Matlab complains and I don't know how to fix it.
Another thing I tried was using UpdateFcn, but a problem with it is that I can only pass one point data by doing event_obj.Position.
Your help will be greatly appreciated.
Thanks,
Eric

Place MATLAB legend such that it does not overlap on the plot

I am generating multiple plots of different datasets in succession using MATLAB. I would like the legend positions to be such that they don't overlap on the plotted lines and it would be ideal if this placement could be done automatically.
I am aware of setting the 'Location' to 'best' to achieve this but the placement of the legend tends to be awkward when 'best' is used (below). Also, I would like the legend to be inside the plot. I also came across a way to make the legend transparent (here) so that it does not render the plotted data invisible, but explicitly placing the legend elsewhere is what I am looking for.
Is there a way to place the legend at the extremes of the image ('NorthWest', 'SouthWest' etc) automatically such that it does not overlap on the plotted data (apart from the methods suggested above)?
So, you have tried using Location instead of Position? For example:
x =1:100;
y = x.^2;
lgd = legend('y = x.^2');
set(lgd,'Location','best')
and you are getting odd results correct? A quick way of solving this would be to still use Location, with best, and extract the coordinates:
lgd.Position
You should get something like this:
ans =
0.7734 0.3037 0.1082 0.0200
which maps to:
[left bottom width height]
You will need to focus on left and bottom. These two values, left and bottom, specify the distance from the lower left corner of the figure to the lower left corner of the legend, and they are analogous to the grid frame you are using.
Then, depending on the size of the frame (I would suggest you use axis([XMIN XMAX YMIN YMAX]) for this, if possible), you can pinpoint the position of the legend within the grid. What you can do next, is check if and which of your graphs in the plot cross paths with the legend (maybe define a relative distance function based on some distance threshold) and if they do, then randomly reposition the legend (i.e. change the values of left and bottom) and repeat until your conditions are met.
If this still troubles you I can write a short snippet. Finally, know that you can always opt for placing the legend on the outside:
set(lgd,'Location','BestOutside')

any way to have mouse stick to a curve in matlab?

I have a java program with a Gaussian plotted in the range -3 to 3 (f = exp(-x^2/3)). I capture the mouse event and have the mouse can only move along the curve so to pick up the value (f) and the corresponding x. Is that possible to implement the same thing in matlab? I search for mouse event in matlab but seems it doesn't have any low-level mouse control or even response.
As suggested by A. Donda, I tried datacursormode. If I have two curves shown on the same figure, I can easily trace the position of the mouse on either curve, I wonder if it is possible to capture the event upon the tracing so I can return the value of the other curve at the same x-coordinate while I am tracing the other curve? Or if it possible to change the way or content the yellow tip shown? What I really interesting is the sum or difference or product of the function values from two curve at the same x position.
Not exactly the same thing, but you can use "Tools / Data Cursor" in the figure window, also accessible by the "yellow note with crosshairs" icon in the toolbar, or the function datacursormode.
You can't easily set the mouse position with matlab (see here for an example). As mentioned by #A. Donda, the straightforward solution is to use the data cursor.
Set the "stick-to-curve" behavior with your mouse
With datacursormode on, you can right click on the axes and set Selection Style to Mouse Position. Then, select a first point on the curve. A data tip containing (x,y) will appear. Select a second point but keep the mouse button down: the data cursor will stick to the curve and will follow the mouse.
Set the "stick-to-curve" behavior programatically
Get the handle of datacursormode and set the SnapToDataVertex property to off before calling datacursormode on
cursorMode = datacursormode(gcf);
set(cursorMode, 'SnapToDataVertex', 'off');
datacursormode on