Problem with Datacursor in candlechart
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Walter Roberson
el 22 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Pie Charts en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!