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.

9 次查看(过去 30 天)
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 7.4.0.287 (R2007a)

采纳的回答

Kenneth Eaton
Kenneth Eaton 2011-1-26
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},...
'High',highArray,...
'Low',lowArray,...
'Close',closeArray,...
'Volume',volumeArray);
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:
setappdata(gca,'AssetData',assetData);
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))]};
end
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);
set(dcmObj,'UpdateFcn',@data_cursor_highlow,'Enable','on');
  3 个评论
Kenneth Eaton
Kenneth Eaton 2011-1-26
Kristen, glad to help. Funny thing is, I actually just figured out how to use the Text Update function myself to answer this other question a couple days ago: http://www.mathworks.com/matlabcentral/answers/78-is-it-possible-to-display-a-crosshair-everytime-a-point-is-marked-with-the-data-cursor-tool-in-matla
Kristen
Kristen 2011-1-27
I tried it out yesterday, "worked like a charm." Thanks so much, again. Now I'm studying the code to understand how it works...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by