matlab数据游标不能使用,李洋:Matlab数据游标(dualcursor)动态展示

这篇博客介绍了如何在Matlab中处理数据游标无法使用的问题,并详细展示了如何通过dualcursor函数动态展示数据游标,包括设置颜色、标记、数据标签和处理多个线条的情况。此外,还涉及到数据标签格式化、光标移动、窗口按钮事件回调函数等内容。
摘要由CSDN通过智能技术生成

for ii=1:length(marker_color)

col_ind = strfind(colors,marker_color(ii));

if ~isempty(col_ind) %It's a color

color = marker_color(ii);

else %Try a marker instead

mark_ind = strfind(markers,marker_color(ii));

if ~isempty(mark_ind)

marker = marker_color(ii);

end;

end;

end;

end;

%Handle default marker and color

if ~exist('color','var'), color = 'r'; end; %set default

if ~exist('marker','var'), marker = '*'; end; %set default

%Add the cursors.

%Ideally, the user specified the 2 x coordinates.

%If not, just put the cursors somewhere nice.

lineh = local_findlines(axh);

%Line data

if isempty(lineh)

x1 = 0;

y1 = 0;

% erasemode = 'normal'; %default

else

%if multiple lines, define callback to allow for selection

set(lineh,'ButtonDownFcn', ...

'setappdata(get(gco,''Parent''),''SelectedLine'',gco);dualcursor(''selectline'',[],[],[],get(gco,''Parent''))');

%set erasemode to xor. This speeds things up a ton with large data sets

% erasemode = get(lineh,'EraseMode');

% set(lineh,'EraseMode','xor');

lineh = min(lineh); %Lets just use one line. This was probably added first

setappdata(axh,'SelectedLine',lineh); %The currently selected line.

%Why the last line? Because it is the first one added

xl = get(lineh,'XData');

yl = get(lineh,'YData');

end;

%Find nearest value on the line

[xv1,yv1] = local_nearest(x_init(1),xl,yl);

[xv2,yv2] = local_nearest(x_init(2),xl,yl);

%Build the string for the data label

textstring1 = feval(datalabelformatfcnh,xv1,yv1);

textstring2 = feval(datalabelformatfcnh,xv2,yv2);

%Add the data label

th1 = text(xv1,yv1,textstring1,'FontSize',8,'Tag','CursorText','Parent',axh);

th2 = text(xv2,yv2,textstring2,'FontSize',8,'Tag','CursorText','Parent',axh);

%For R13 or higher (MATLAB 6.5), use a background color on the text string

v=ver('MATLAB');

v=str2num(v.Version(1:3));

if v>=6.5

set(th1,'BackgroundColor','y');

set(th2,'BackgroundColor','y');

end;

yl = ylim(axh);

lim = localObjbounds(axh);

lim = lim(3:4); %y values only

yl(isinf(yl)) = lim(isinf(yl));

%Add the cursors

ph1 = line([xv1 xv1 xv1],[yl(1) yv1 yl(2)], ...

'Color',color, ...

'Marker',marker, ...

'Tag','Cursor', ...

'UserData',[lineh th1], ...

'LineStyle','-', ...

'Parent',axh);

ph2 = line([xv2 xv2 xv2],[yl(1) yv2 yl(2)], ...

'Color',color, ...

'Marker',marker, ...

'Tag','Cursor', ...

'UserData',[lineh th2], ...

'LineStyle','-', ...

'Parent',axh);

%Add context menu to the cursors

% cmenu = uicontextmenu('Parent',get(axh,'Parent'));

cmenu = uicontextmenu('Parent',hFig);

set([ph1 ph2],'UIContextMenu',cmenu);

% Define the context menu items

item1 = uimenu(cmenu, 'Label', 'Export region to workspace', ...

'Callback', 'dualcursor(''exportws'',[],[],[],get(gco,''Parent''))');

item2 = uimenu(cmenu, 'Label', 'Export region to new figure', ...

'Callback', 'dualcursor(''exportfig'',[],[],[],get(gco,''Parent''))');

item3 = uimenu(cmenu, 'Label', 'Export cursor data to workspace', ...

'Callback', 'dualcursor(''exportcursor'',[],[],[],get(gco,''Parent''));');

item4 = uimenu(cmenu, 'Label', 'Turn cursors off', ...

'Callback', 'dualcursor(''off'',[],[],[],get(gco,''Parent''))', ...

'Separator','on');

setappdata(th1,'Coordinates',[xv1 yv1])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值