How can I customly fix the Data cursor location in the Matlab plot?

28 次查看(过去 30 天)
I have a plot (attached). In this plot I need to add two data cursors. In one of the data cursor, I need to fix the X value to be 0 and in the other i need to fix the y value to 0. How can I adjust the data cursor points to do so? currently in the first data tip, the Y value is -49.51 and in the other data tip the x value is 0.485. I need to make it in such a way as to keep Y value 0 in first and X value 0 in second. By doing this I will get the corresponding X value from 1st and corresponding Y value from 2nd
  2 个评论
Jaikrishnan Mahalekshmi Sahasranamam
Thankyou. I am now trying to plot a simple code where x=2y. I am using the code block from this link to create and fix the position of the data cursor.
Below is the my code
clc
clear
close
x=linspace(0,10,10);
y=2*x;
%%
% First plot the data
hLine = plot(x,y);
% First get the figure's data-cursor mode, activate it, and set some of its properties
cursorMode = datacursormode(gcf);
set(cursorMode, 'enable','on', 'UpdateFcn',@setDataTipTxt);
% Note: the optional @setDataTipTxt is used to customize the data-tip's content text
% Note: the following code was adapted from %matlabroot%\toolbox\matlab\graphics\datacursormode.m
% Create a new data tip
hTarget = handle(hLine);
hDatatip = cursorMode.createDatatip(hTarget);
% Create a copy of the context menu for the datatip:
set(hDatatip,'UIContextMenu',get(cursorMode,'UIContextMenu'));
set(hDatatip,'HandleVisibility','off');
set(hDatatip,'Host',hTarget);
set(hDatatip,'ViewStyle','datatip');
% Set the data-tip orientation to top-right rather than auto
set(hDatatip,'OrientationMode','manual');
set(hDatatip,'Orientation','top-right');
% Update the datatip marker appearance
set(hDatatip, 'MarkerSize',5, 'MarkerFaceColor','none', ...
'MarkerEdgeColor','k', 'Marker','o', 'HitTest','off');
% Move the datatip to the right-most data vertex point
position = [x(end),y(end),1; x(end),y(end),-1];
% position = [x(1),y; x(1),y];
update(hDatatip, position);
So this will basically set the datacursor to the end vertex of both x and y. ie at x=10 and y=20. But I donot get coordinates specified in the datacursor as X:10 and Y:20. Rather I am getting an error stated in the plot figure below. Should I change something in the editTextupdateFunction of the datatip?
Also I would like to set the datacursor to the initial position of x like this
position = [x(1),y; x(1),y];
But this also is throwing error - "Error in custom datatip string function". Why is this case. What should I modify in the code?
datacur.png

请先登录,再进行评论。

采纳的回答

Dinesh Yadav
Dinesh Yadav 2019-8-2
I am assuming you have the equation used for plotting. You can feed the value “x=0” in the function to get the value of “y” and vice versa and store these values in variable “pos1” and “pos2”. Without having the values for both “x” and “y” you cannot obtain the DataCursor on the plot.
I am attaching a slightly modified code for your given code with which you can obtain DataCursor at any point of your choice. I have commented few lines that are not needed and changed few lines of code for updating DataCursors.
  1 个评论
Jaikrishnan Mahalekshmi Sahasranamam
Thank you Dinesh. Now I think I can update the code based on the postition I wish. If I have some queries, I will followup for this. Thanks a lot for your help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by