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
Related
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')
Is there any way in Matlab to specify that the tick marks should appear on top of the graph?
I'm making a plot with shading for when recessions occur. I currently plot a patch object as the farthest back object in a plot, but this obscures any tick marks that occur during this window. For example:
Is there a way to get the tick marks to appear in front of this shading?
assuming you first draw the patch then the bar plot, this is what a solution would look like:
h=gca;
h.Layer='top'
This uses the layer property of the axes to force the bar plot to be on top.
If you have an older matlab version you might want to add this instead:
set(gca,'Layer','top')
I create a figure with toggle button on its toolbar by uitoggletool. The callback function for it is shown below:
function calc_slope(handle,event)
on = get(handle,'State');
if strcmpi(on,'on') || strcmpi(on,'off'),
xy=imline;
addNewPositionCallback(xy,#(xy)...
title(['\DeltaY/\DeltaX = ',num2str((xy(4)-xy(3))/(xy(2)-xy(1))),...
'[\DeltaX = ',num2str(xy(2)-xy(1)),...
',\DeltaY = ',num2str((xy(4)-xy(3))),']']));
end
As you can see, I'm trying to calculate the slope between two points on a curve. The code is something I found in the web that's pretty close to what I'm trying to. The 'addNewPositionCallback" is executed in both 'On' and 'Off' state, but it's merely a means to repeat the process as many times as a user wants. What I really want to do is number 4 and 5 below.
What I really want to do is as follows:
"Draggable" points "snapped" to the nearest curve
The connecting line shows
Delta X, delta Y, and the slope provided in a "text box" drawn near
the curve
Draw as many lines as needed while in the 'On' state
Delete all lines and 'text' boxes when toggle button is set to 'Off'
The imline function gives a bunch of features such as different line colors. I can care less on those. The major "wants" are the snapping and the text box.
Your help will be greatly appreciated.
Thank you,
Eric
I want to take input coordinates through mouse in Matlab oR Simulink, there is no built-in facility in Matlab of input of 3D coordinate through mouse device, however the buit-in function ginput can only store 2D coordinates of mouse, is there is any possibility in MATLAB/SIMULINK to input 3D coordinates through mouse device?
If I understand you correctly, you want to get the coordinates (in data space) of the mouse click, when the plot is 3D. That is, you click somewhere in the plot and it returns your current position. I have actually tackled this exact problem before.
The main difficulty with this task--and the other posters have alluded to it--is that you are clicking on a 2D screen. Thus, you cannot specify 3 independent positions on a 2D screen uniquely. Rather, clicking on the screen defines a line segment, normal to the plane of the screen, and any of the 3D points along this line are equally valid. Do you understand why this is the case?
To demonstrate, try this short example in Matlab:
surf(peaks); %draw a sample plot
keydown = 2;
while keydown ~= 0,
disp('Click some where on the figure');
keydown = waitforbuttonpress;
end
currPt = get(gca,'CurrentPoint');
disp(currPt);
You'll observe that currPt is a 2x3 matrix. This defines the start and end points of this line. Let's plot this line now:
hold on;
plot3( currPt(:,1), currPt(:,2), currPt(:,3), 'k-', 'LineWidth', 2);
view(-19,46); %rotate to view this line
So the question is: how to define which point along this line you want to select? Well the answer depends on what type of data you have in the first place. If you have point data, choosing one of your vertices exactly can be tricky, and you might need to do some post-processing of your data (for example, to calculate the closest point in your dataset to the currPt line). If you have patch or surface data (such as this example), this is just the intersection of a line and a plane.
There are some tools on the File Exchange to get 3D points for various datasets. One that I just found is: http://www.mathworks.com/matlabcentral/fileexchange/7594-click3dpoint
I am trying to get the y coordinates on a xy plot by clicking on the plot. This is very similar to the function ginput which will generate a cursor and by clicking on the plot you can get the x and y coordinates where the cursor is placed. However, for my particular purpose, I want to have a line cross the figure instead of cursor as a guide of determining the right y value to use. Is there any function in matlab can do this? If not, what is the way to do it? Any help is greatly appreciated.
I don't believe there is a pre-packaged function for that, but you can do it using the strategies described in this video tutorial from Doug Hull, with slight modifications.