MATLAB Answers

Need help writing a datacursormode Text Update Function to display high, low, close, date, & volume data as a Datatip on a plot made with the highlow function of the Financial Toolbox.

13 views (last 30 days)
Kristen on 26 Jan 2011
I have high, low, and close prices plotted against index no. on the x-axis (I use index no. rather than serial date number to eliminate gaps in the plot at each weekend where there are no price data). I wish to utilize a Text Update Function that picks up on the cursor's x-position only, regardless of y-position, and displays a Datatip with labels and values as follows:
Date: 01/26/2011
High: 23.75
Low: 22.15
Close: 23.20
Vol: 327000
The string date and volume are not even plotted, but should be picked up using the index from the plot (the x-position of the cursor) and finding the values in a separate matrix (from the high-low-close values) by using that index.
It is common to display the datatips this way on online plots of historical stock price data; I am hopeful someone has already developed such a Text Update Function for these plots in Matlab.
I have Matlab version (R2007a)

Accepted Answer

Kenneth Eaton
Kenneth Eaton on 26 Jan 2011
Since you want your data cursor to display data that is not on the plot, you will have to put that data in a place that the Text Update function can find it. A convenient object to store the data with is the plot axes, and you can either store the data in the 'UserData' field or use the function SETAPPDATA to attach the data to the axes object.
The first step which will make things easier is to collect your data into a structure:
assetData = struct('Date',{dateStrings},...
where dateStrings is a cell array of strings containing dates and the other field values are numeric arrays of data. After making your HIGHLOW plot, you can attach this data to the current axes like so:
Next, the Text Update function can be designed to get the target object (i.e. plot line) that was clicked on, get its parent object (i.e. the axes), then get the data we attached to it using the function GETAPPDATA. This data can then be used to create the data cursor text. Here is the new Text Update function data_cursor_highlow:
function output_txt = data_cursor_highlow(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).
% Get the parent of the target object (i.e. the axes):
hAxes = get(get(event_obj,'Target'),'Parent');
% Get the data stored with the axes object:
assetData = getappdata(hAxes,'AssetData');
% Get the data index (i.e. the x position):
pos = get(event_obj,'Position');
index = pos(1);
% Create the data cursor text string:
output_txt = {['Date: ',assetData.Date{index}],...
['High: ',num2str(assetData.High(index))],...
['Low: ',num2str(assetData.Low(index))],...
['Close: ',num2str(assetData.Close(index))],...
['Vol: ',num2str(assetData.Volume(index))]};
You can start using this Text Update function in your plot in one of two ways:
  • Via the UI: Turn on the Data Cursor tool by pressing the button on the figure menu, create a new datatip, right click on the datatip, select the menu option "Select Text Update Function...", and then select the data_cursor_highlow m-file.
  • Via code: Run the following code after plotting your data:
dcmObj = datacursormode(gcf);
Kristen on 27 Jan 2011
I tried it out yesterday, "worked like a charm." Thanks so much, again. Now I'm studying the code to understand how it works...

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!