Skip to Main Content Skip to Search
Accelerating the pace of engineering and science

 

Newsletters - MATLAB Digest

Data Streaming from the Instrument Control Toolbox to an Excel Spreadsheet


Putting a MATLAB Array Into Excel

Next, you create labels for the range of spreadsheet cells that will contain the MATLAB array. The first step is to get the length of the data vector. You will apply the length when creating the spreadsheet cell labels.

l=length(data);

Next you create the string labels.

lastcell = ['A' num2str(l)];

firstcell = ['A' '1'];

The next step is to put a MATLAB array into Excel. You need to get a handle to the range of cells where the array will be written. Then you set its Value property to the data array you have from the MATLAB workspace.

ActivesheetRange = get(Activesheet,'Range',firstcell,lastcell);

set(ActivesheetRange, 'Value', data);

Adding a Chart and Defining Its Appearance

You will plot the data in a chart in Excel. A Chart can be an element of a workbook just as a spreadsheet is. To add a chart to the workbook, you first get a handle to the Charts interface and then invoke its Add method.

Charts = Workbook.Charts;

Chart = invoke(Charts,'Add');

You need to set the chart type to be a line plot. To do this, set the CharType property to 65 (the Excel documentation describes other chart types). You also want the plot to be shown as part of Sheet1 instead of being shown as an independent sheet. Invoke the Location method on the Chart Interface, which takes two additional parameters. Argument 3 defines the chart as an object to be inserted in a sheet and argument 4 defines the sheet you chose, in this case Sheet1.

set(Chart,'ChartType',65);

Chart = invoke(Chart,'Location',2,'Sheet1');

Setting the Title and Axes Properties

To add a title to the chart you must first set its HasTitle property to one. The Caption property of the ChartTitle object holds the title name.

set(Chart,'HasTitle',1);

set(Chart.ChartTitle,'Caption','Filtered Oscilloscope Data');

To add an x-axis label to the plot, you need to first invoke the Axes method of the Chart interface with parameter 1 to get a handle to the x-axis object. You then set the x-axis HasTitle property to 1, and the Caption to the axis name.

x = invoke(Chart,'Axes',1);

set(x,''HasTitle',1);

set(x.AxisTitle,'Caption','Sample Number');

A similar procedure is followed for adding the y-axis label. The Axes method of the Chart is now invoked with parameter value 2.

y = invoke(Chart,'Axes',2);

set(y,'HasTitle',1);

set(y.AxisTitle,'Caption','Magnitude (V)');

Part 4

Contact sales
Subscribe to newsletters