主要内容

Read Spreadsheet Data UsingExcelas Automation Server

此示例显示了如何使用COM Automation Server从MATLAB访问另一个应用程序®。它创建一个用户界面以访问数据微软®Excel®文件。如果您不使用应用程序中的组件对象模型(COM),请参见电子表格s对于将Excel电子表格数据导入MATLAB的替代方案。

To enable the communication between MATLAB and the spreadsheet program, this example creates an object in an Automation server running an Excel application. MATLAB then accesses the data in the spreadsheet through the interfaces provided by the Excel Automation server. Finally, the example creates a user interface to access the data in aMicrosoft Excel文件。

Techniques Demonstrated

  • 使用自动化服务器访问MATLAB的另一个应用程序

  • 将Excel数据操纵到接口中使用的类型和绘图的方法

以下技术演示了如何可视化和操纵电子表格数据:

  • 实现接口,该接口可以绘制Excel电子表格的选定列。

  • Insertion of a MATLAB figure into an Excel file.

要查看完整的代码列表,请打开文件actx_excel.m在编辑中。

创造Excel自动化服务器

从MATLAB访问电子表格数据的第一步是使用该应用程序在自动化服务器过程中运行Excel应用程序actxserver功能和程序ID,Excel.Application

exl = actxserver('excel.application');

exl对象提供对Excel程序支持的许多接口的访问。万博1manbetx使用Workbooks接口打开包含数据的Excel文件。

exlwkbk = exl.workbooks;exlfile = exlwkbk.open([[docroot'/techdoc/matlab_external/examples/input_resp_data.xls']);

使用workbookSheetsinterface to access the data from a范围对象,该对象在指定表中存储了对一系列数据的引用。此示例访问列中第一个单元格的所有数据Ato the last cell in columnG

exlSheet1 = exlFile.Sheets.Item('Sheet1'); robj = exlSheet1.Columns.End(4); % Find the end of the column numrows = robj.row; % And determine what row it is dat_range = ['A1:G' num2str(numrows)]; % Read to the last row rngObj = exlSheet1.Range(dat_range);

此时,来自Excel文件的整个数据集sheet1is accessed via the range object interfacerngobj。This object returns the data in a MATLAB cell arrayexlData,其中包含数字和字符数据:

exldata = rngobj.value;

Manipulate Data inMATLAB工作区

现在,数据在单元格数组中,您可以使用MATLAB函数来提取和重塑数据中的一部分以在接口中使用并传递到绘图函数。有关数据的假设,请参见Excel Spreadsheet Format

这following code manipulates the data:

对于II = 1:size(exldata,2)matdata(:,ii)= reshape([[exldata {2:en​​d,ii}],size(exldata(2:end,ii)));lboxList {ii} = [exldata {1,ii}];结尾

这code performs the following operations:

  • 从单元格数组中提取数字数据。查看卷曲括号内的索引表达式{}

  • 串联索引操作返回的单个双打。查看方括号内的表达式[]

  • Reshapes the results into an array that arranges the data in columns using thereshapefunction.

  • Extracts the text in the first cell in each column ofexlData数据并将文本存储在单元格数组中lBoxList。该变量用于通用电气nerate the items in the list box.

Excel电子表格格式

如本图像所示,此示例假设Excel电子表格的特定组织。

input_resp_data.xls电子表格中的数据。

这format of the Excel file is:

  • 这first element in each column is text that identifies the data contained in the column. These values are extracted and used to populate the list box.

  • 第一列Timeis used for thex-axis of all plots of the remaining data.

  • All rows in each column are read into MATLAB.

创建绘图器接口

This example uses an interface that enables you to select from a list of input and response data. All data is plotted as a function of time and you can continue to add more data to the graph. Each data plot added to the graph causes the legend to expand.

该界面包括以下详细信息:

  • Legend that updates as you add data to a graph

  • Clear button that enables you to clear all graphs from the axes

  • 保存将图作为png文件并将其添加到另一个Excel文件中的按钮

  • Toggle button that shows or hides the Excel file being accessed

  • 图删除功能以终止自动化服务器

选择并绘制数据

当您单击创建图button, its callback function queries the list box to determine what items are selected and plots each data versus time. MATLAB updates the legend to display new data while still maintaining the legend for the existing data.

函数plotbuttonCallback(src,evnt)ISEREDED = get(listBox,'value');网格(a,'on');将全部固定为p = 1:长度(偶像选择)交换机(p)案例1绘图(a,tme,matdata(::,2))案例2图(a,tme,matdata(a,tme,matdata)(:,3))案例3图(a,tme,matdata(::4))案例4图(a,tme,matdata(::,5))案例5图(a,tme,matdata(:,6))案例6图(a,tme,matdata(::,7)),否则disp(“选择数据绘制数据”)端端[b,c,g,lbs] = legend([lbs lboxlist(lbs lboxlist(iSeledecre+1)]);结束%plotbuttonCallback

清除轴

绘图仪设计为在用户从列表框中选择数据时不断添加图。这Clear Graph按钮清除并重置轴并清除用于存储图数据标签的变量(图例使用)。

清除按钮函数ClearButtonCallback(SRC,EVT)CLA(a,'reset')lbs =''';结束%clearButtonCallback

显示或隐藏Excel文件

MATLAB程序可以访问自动化服务器中运行的Excel应用程序的属性。通过设置可见的property to1或者0, this callback controls the visibility of the Excel file.

%%显示或隐藏excel文件函数dispbuttoncallback(src,evt)exl.visible = get(src,'value');结束%dispbuttoncallback

Close Figure and TerminateExcelAutomation Process

Since the Excel Automation server runs in a separate process from MATLAB, you must terminate this process explicitly. There is no reason to keep this process running after closing the interface, so this example uses the figure's删除function to terminate the Excel process with theQuit方法。您还需要终止用于保存图形的Excel过程。有关终止此过程的信息,请参阅将MATLAB图插入Excel电子表格中

%% Terminate Excel processes function deleteFig(src,evt) exlWkbk.Close exlWkbk2.Close exl.Quit exl2.Quit end % deleteFig

InsertMATLABGraphs intoExcel电子表格

您可以将使用此接口创建的图形保存在Excel文件中。本示例为此目的使用单独的Excel Automation Server进程。回调保存图push button creates the image and adds it to an Excel file:

  • Both the axes and legend are copied to an invisible figure configured to print the graph as you see it on the screen (figurePaperPositionMode属性设置为汽车)。

  • 打印command creates the PNG image.

  • 使用Shapes接口以在Excel工作簿中插入图像。

这server and interfaces are instanced during the initialization phase:

exl2 = actxserver('excel.application'); exlWkbk2 = exl2.Workbooks; wb = invoke(exlWkbk2,'Add'); graphSheet = invoke(wb.Sheets,'Add'); Shapes = graphSheet.Shapes;

使用此代码实现保存图按钮回调:

函数saveButtonCallback(src,evt)tempfig = fige('可见','off','paperpositionmode','auto');tempfigfile = [tempname'.png'];ah = findobj(f,'type','axes');copyObj(ah,tempfig)%复制图形轴和图例轴打印(tempfig,'-dpng',tempfigfile);shapes.addpicture(tempfigfile,0,1,50,18,300,235);Exl2.Visible = 1;结尾

Run Example

要运行示例,请在列表框中选择任何项目,然后单击创建图按钮。此示例提供的示例数据包含三个输入和三个相关的响应数据集。所有这些数据集均与Excel文件中的第一列绘制,即时间数据。

View the Excel data file by clicking the显示Excel数据文件按钮。要将图像保存在其他Excel文件中,请单击保存图按钮。如果您在当前文件夹中有写入权限,则保存图选项在该文件夹中创建一个临时PNG文件。

此图显示了在列表框中选择的输入/响应对并绘制在轴上的接口。

Excel绘图器示例输出。

要运行此示例,请单击此示例关联

See Also

相关话题