Problem with Datacursor in candlechart

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 22 Nov 2012
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.
  1 Comment
gaia buratti
gaia buratti on 2 Dec 2012
Thanks Walter, you're right, indeed after some trial I managed to change the function candle() such that this insert insert the datacursor for the candle chart

Sign in to comment.

More Answers (0)

Categories

Find more on Geographic Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!