Problem with Datacursor in candlechart
7 次查看(过去 30 天)
显示 更早的评论
Hello! I have this problem using the candle chart. Plotting the Stock prices using
if true
%candle(HI, LO, CL, OP)
end
the datacursor works fine, it shows me X and Y value ( I would rather to have also th e daate, but it is a second step)
Then I need to plot other lines, for which I update the datacursor to show dates as well using the following function:
if true
% function dcmH = customDataCursor(h, datalabels)
% Example:
% % generate some data and chart it
% N = 20;
% x = rand(N,1);
% y = rand(N,1);
% h = plot(x,y,'ko');
% % make up some labels and apply them to the chart
% labels = cell(N,1);
% for ii = 1:N
% labels{ii} = ii;
% end
% customDataCursor(h,labels)
% Check input arguments
if ~exist('h','var')||~exist('datalabels','var')
error('Improper inputs to function customDataCursor')
elseif ~ishandle(h)
error('Nonexistent handle passed to function customDataCursor')
elseif length(datalabels) ~= length(get(h,'xdata'))
error(['Error in input to function customDataCursor: '...
'number of labels is different than the number of data points in the line'])
end
% Put the labels in the 'userdata' property of the line
set(h,'userdata',datalabels)
% find the handle for the data cursor; set the update function
dcmH = datacursormode(gcf);
set(dcmH,'UpdateFcn',@cursorUpdateFcn)
function output_txt = cursorUpdateFcn(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
% position of the data point to label
pos = get(event_obj,'Position');
% read in the labels from 'UserData' of the line
labels = get(get(event_obj,'Target'),'UserData');
% read the x and y data
xvals = get(get(event_obj,'Target'),'XData');
yvals = get(get(event_obj,'Target'),'YData');
% now figure out which data point was selected
datapoint = find( (xvals==pos(1))&(yvals==pos(2)) );
% create the text to be displayed
output_txt = { labels{datapoint};...
['X: ',num2str(pos(1),4)];...
['Y: ',num2str(pos(2),4)] };
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
end
end
end
Now if I put the cursor on the new line it is fantastic and shows me the dates, but if I position it on the candlechart it gives me an error like this:
"Error in custom datatip string function"
Someone can help me on this?
thanks
Gaia
0 个评论
采纳的回答
Walter Roberson
2012-11-22
If no point is found that matches those criteria, then "datapoint" will come out empty. Meanwhile, as there is probably no UserData there, labels will have been assigned the empty array [] and not the empty cell array {} . Then labels{datapoint} is an error.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Pie Charts 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!