Examples

www.kxcad.net Home > CAE Software Index > MATLAB Index >



Fetching Securities Data and Displaying It in a Table

This example shows how to create a component that fetches securities data and displays it in a report as a table.

  1. Create a component named Equity Values in the class directory named CStockTicker.

  2. Give the component one string property named Ticker, and specify its attributes.

    1. In the Options pane of the Report Explorer, double-click New_String_Property.

    2. For Property name, specify a valid MATLAB variable name.

    3. Specify the property's data type. In this case, Ticker is a string value, which is the default data type.

    4. Specify the property's default value.

      Because this example fetches ticker values for the security Google, set the Default value to 'GOOG'. (The single quotation marks are required to make this a string value.)

      Your specified settings appear in the Code Preview pane.

  3. To build the new component, click the Build button in the Report Explorer. The Equity Values component now appears in the Options pane in the middle of the Report Explorer.

  4. Edit the component's execute.m file to retrieve stock market data and display it in a table in the generated report.

    1. In the @CStockTicker directory, open execute.m.

    2. Enter the following text into execute.m.

      function out=execute(thisComp,parentDoc,varargin)
       
      stockQuote = fetch(GOOG, thisComp.Ticker);
      stockQuote.Date = datestr(stockQuote.Date,1);
      stockQuote.Time = datestr(stockQuote.Time,13);
      out = execute(rptgen.cfr_table(...
           'Source', stockQuote,...
           'numHeaderRows', 0,...
           'TableTitle', 'Stock Market Pricing Data'),...
          parentDoc);
      
  5. Append the security symbol, goog, to the component name. Enter the following text into getOutlineString.m.

    function olstring=getOutlineString(thisComp)
     
    olstring = [getName(thisComp),' - ',thisComp.Ticker];
    

    The component name now appears as Equity Values — goog.

  6. Modify the getdialog boxeschema.m file to change the appearance of the Properties pane. Enter the following text into this file to display the last quoted price for the security in the Properties pane.

    function dlgStruct = getdialog boxeschema(thisComp, name)
    
    try
        currQuote = fetch(yahoo, thisComp.Ticker);
        quoteStr = sprintf('Last value: %g', currQuote.Last);
    catch
        quoteStr = sprintf('Warning: ... 
        "%s" is not a valid symbol.', thisComp.Ticker);
    end
    
    dlgStruct = thisComp.dlgMain(name,...
        thisComp.dlgContainer({
          thisComp.dlgWidget('Ticker',...
            'DialogRefresh',true,...
            'RowSpan',[1 1],'ColSpan',[1 1]);
          thisComp.dlgText(quoteStr,...
            'RowSpan',[2 2],'ColSpan',[1 1]);
        },'Stock',...
          'LayoutGrid',[3 2],...
          'RowStretch',[0 0 1],...
          'ColStretch',[0 1]));
    

    The Properties pane for the component, Equity Values, now looks as follows.

  7. Click File > Report to generate the report. The following output appears in the report.

Displaying Securities Data in Two Tables

This example, which shows how to use multiple properties within a component, expands upon Fetching Securities Data and Displaying It in a Table.

  1. Create a new report template and save it as stockreport.rpt. Add two Equity Values components to the template.

  2. Edit the entry in the New marker property field to change the ticker property of the second component to '^GSPC' (S&P 500 index).

  3. Run the report.

    The report displays two tables of data, one for Google and another for the S&P 500 index.

  


© 1984-2007 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments