用Excel Link实现Excel与Matlab溷合编程
- 格式:docx
- 大小:16.15 KB
- 文档页数:1
如何将Excel中的数据导入MATLAB并在MATLAB中画出图像
1.如何将Excel中的数据在MATLAB界面下显示出来:
首先Excel必须是office的(WPS的不可以,MATLAB不能识别),在MATLAB中使用命令xlsread读取Excel中的数据到MATLAB里,如下所示:[number,txt,raw]=xlsread('noise.xlsx'),此时读取的文件须在MATLAB默认路径下,命令具体用法及参数含义可直接用help命令在MATLAB中查找。
读取的数据界面如下:
2.如何将Excel中的数据导入MATLAB并在MATLAB中画出图像?
首先Excel必须是office的(WPS的不可以,MATLAB不能识别),file-Import Data-在工作路径下找到需要导入的文件(图3)-
点击打开(图4)-点击next-点击finish-导入成功-在workspace中看到图5(导入的数据)。
至此数据导入成功。
下面是如何利用该数据绘图:
法一:双击workspace中的data打开如图6,选中要画图的数据如图7(数据范围可以根据需要随便选择),点击上方的plot(data(1:874,1:2),画出图像如图8,并且在工作空间中自动显示画图命令。
法二:直接在工作空间中写画图命令。
图3
图4
图5
图6
图7
图8。
首先Excel必须是office的(WPS的不可以,MATLAB不能识别),在MATLAB中使用命令xlsread读取Excel中的数据到MATLAB里,如下所示:[number,txt,raw]=xlsread(''),此时读取的文件须在MATLAB默认路径下,命令具体用法及参数含义可直接用help命令在MATLAB中查找。
读取的数据界面如下:
1.如何将Excel中的数据导入MATLAB并在MATLAB中画出图像?
首先Excel必须是office的(WPS的不可以,MATLAB不能识别),file-Import Data-在工作路径下找到需要导入的文件(图3)-点击打开(图4)-点击next-点击finish-导入成功-在workspace中看到图5(导入的数据)。
至此数据导入成功。
下面是如何利用该数据绘图:
法一:双击workspace中的data打开如图6,选中要画图的数据如图7(数据范围可以根据需要随便选择),点击上方的plot(data(1:874,1:2),画出图像如图8,并且在工作空间中自动显示画图命令。
法二:直接在工作空间中写画图命令。
图3
图4
图5图6图7图8。
matlab与excel数据交换的有利工具——excel link(1) What Is Excel Link?Excel Link is a software add-in that integrates Microsoft Excel andMATLAB in a Microsoft Windows-based computing environment. By connecting Exceland MA TLAB, you can access the numerical, computational, and graphical powerof MATLAB from Excel worksheet and macro programming tools. Excel Link letsyou exchange and synchronize data between the two environments.1. System Requirements2. Installing Excel Link3. Configuring Excel to Work with Excel Link4. Starting Excel Link5. Connecting to an Existing MATLAB Session6. Stopping Excel LinkFollow these instructions to install Excel Link and then configure Excel.System RequirementsExcel Link requires approximately 202 kilobytes of disk space. Operating system requirements are:Microsoft Windows XP ,Microsoft Windows 2000Excel Link also requires one of the following versions of Excel:Excel 2000Excel ,2002Excel ,2003Excel(Excel Link has also been qualified against Excel 2007 Beta.)Excel Link also requires MA TLAB for Windows.For best results with MATLAB figures and graphics, set the color palette of your display to a value greater than 256 colors. Click Start > Settings > Control Panel > Display, and then click the Settings tab.Choose an appropriate entry from the Color Palette menu.Installing Excel LinkInstallWindows and Excel before you install MATLAB and Excel Link.To install Excel Link, follow the instructions in the MA TLAB installation documentation. Select the Excel Link check box when you select MATLAB components to install.Configuring Excel to Work with Excel LinkAfter you have installed Excel Link, you are ready to configure Excel. You need do these steps only once:1. Start Microsoft Excel.2. Select Tools > Add-Ins and click Browse.3. Find and select the Excel Link add-in excllink.xla under matlabroot/toolbox/exlink. Click OK.(Throughout this document the notation matlabroot representsthe MATLAB root directory, the directory where MATLAB is installed on yoursystem.)4. Back in the Add-Ins dialog box, make sure that the check box is selected and click OK.The Excel Link add-in loads now and with each subsequent invocation of Excel.5 .Watch for the appearance of the MATLAB Command Window button on the Windows taskbar.6 .Watch for the appearance of the Excel Link toolbar on your Excel worksheet.Excel Link is now ready for your use.Starting Excel LinkAutomatic StartWhen installed and configured according to the preceding instructions,Excel Link and MA TLAB automatically start when you start Excel.If you do not want Excel Link and MATLAB to start automatically when you start Excel, enter =MLAutoStart("no") in a worksheet cell. This function changes the initialization file so that Excel Link and MATLAB no longer start automatically when you start Excel. See MLAutoStart in Functions — Alphabetical List.Manual StartTo start Excel Link and MATLAB manually from Excel, select Tools > Macro. In the Macro Name/Reference box enter matlabinit and click Run. Watch for the MA TLAB Command Window button to appear on the taskbar. See matlabinit in Functions — Alphabetical List.Connecting to an Existing MATLAB SessionTo connect a new Excel session to an existing MATLAB process, you must start MATLAB with the /automation command-line option. The /automation option starts MA TLAB as an automation server. The Command Window is minimized, and the MATLAB desktop is not running.To add the /automation option to the command line:1. Right-click your shortcut to MA TLAB.2 .Select Properties.3. Click the Shortcut tab.4. Add the string /automation in the Target field. Remember to leave a space between matlab.exe and /automation.Stopping Excel LinkTo stop both Excel Link and MATLAB, stop Excel as you normally would.Excel Link and MATLAB both stop when you stop Excel.To stop MA TLAB and Excel Link and leave Excel running, enter =MLClose() inan Excel worksheet cell. You can restart Excel Link and MATLAB manually with MLOpen or matlabinit.If you stop MATLAB directly in the MATLAB Command Window and leave Excel running, enter =MLClose() in an Excel worksheet cell. (MLClose tells Excel that MATLAB is no longer running.)You can restart Excel Link and MA TLAB manually with MLOpen or matlabinit. Understanding the EnvironmentExcel Link communicates between the Excel workspace and the MATLAB workspace.It positions Excel as a front end to MA TLAB. You use Excel Link functionsfrom an Excel worksheet or macro without leaving the Excel environment. Witha small number of functions to manage the link and manipulate data, ExcelLink is powerful in its simplicity.Installing and Operating Excel LinkThis section covers the following topics:matlab与excel数据交换的有利工具——excel link(2) What the Functions DoThis section covers the following topics:Link Management FunctionsData Management FunctionsWith Excel Link, Microsoft Excel becomes an easy-to-use data-storage and application-development front end for MA TLAB, which is a powerful computational and graphical processor. Excel Link provides functions to manage the link and to manipulate data. You never have to leave the Excel environment. You can invoke functions as worksheet cell formulas or in macros.For details on each function, see Functions — Alphabetical List.Link Management FunctionsExcel Link provides four link management functions to initialize, start,and stop Excel Link and MATLAB.You can invoke any link management function except matlabinit as a worksheet cell formula or in a macro. You invoke matlabinit fromthe Excel Tools Macro menu or in a macro subroutine.Use MLAutoStart to toggle automatic startup. If youinstall and configure Excel Link according to the default instructions, Excel Link and MATLAB automatically start every time you start Excel. If you choose manual startup, use matlabinit to initialize Excel Link and start MATLAB.Use MLClose to stop MA TLAB .without stopping Excel,and use MLOpen or matlabinit to restart MATLAB in the same Excel session.Data Management FunctionsExcel Link provides the following data management functions to copy data between Excel and MATLAB and to execute MATLAB commands from Excel.Function Purposematlabfcn Evaluate MA TLAB command given Excel data.matlabsub Evaluate MATLAB command given Excel data and designate output location. MLAppendMatrix Create or append MA TLAB matrix with data from Excel worksheet. MLDeleteMatrix Delete MA TLAB matrix.MLEvalString Evaluate command in MATLAB.MLGetFigure Import current MATLAB figure into Excel spreadsheet.MLGetMatrix Write contents of MA TLAB matrix in Excel worksheet.MLGetVar Write contents of MATLAB matrix in Excel VBA variable.MLPutMatrix Create or overwrite MATLAB matrix with data from Excel worksheet.MLPutVar Create or overwrite MATLAB matrix with data from Excel VBA variable. MLShowMatlabErrors Used by MLEvalString to return standard Excel Link errors or full MATLAB errors.MLStartDir Specify current working directory of MATLAB after startup.MLUseFullDesktop Specify whether to use full MA TLAB desktop or only Command window. You can invoke any data management function except MLPutVar as a worksheet cell formula or in a macro. You can invoke MLPutVar only in a macro.Use MLAppendMatrix, MLPutMatrix, and MLPutVar to copy data from Excel to MA TLAB.Use MLEvalString to execute MA TLAB commands from Excel.Use MLDeleteMatrix to delete a MATLAB variable.Use matlabfcn, matlabsub, MLGetMatrix,and MLGetVar to copy data from MATLAB to Excel.matlab与excel数据交换的有利工具——excel link(3) Tips and RemindersThis section covers the following topics:Syntax 语法Worksheets 工作表Macros 宏Data Types 数据类型Dates 日期Saved Worksheets 保存工作表Information for International Users 国际用户信息These tips and reminders help you use Excel Link efficiently.Excel Link functions perform an action, while Microsoft Excel functions return a value. Keep this distinction in mind as you use Excel Link. Excel operations and function keys may behave differently with Excel Link functions.Syntax 语法Function NamesExcel Link function names are not case sensitive; that is, MLPutMatrix and mlputmatrix are the same.MATLAB function names and variable names are case sensitive; that is, BONDS, Bonds, and bonds are three different MA TLAB variables. Standard MATLAB function names are always lower case; for example, plot(f).Worksheet Formulas1. Begin worksheet formulas with + or =. For example:=mlputmatrix("a", C10)2 .In worksheet formulas, enclose function arguments in parentheses. In macros, leave a space between the function name and the first argument; do not use parentheses.Variable Names1. You can directly or indirectly specify a variable-name argument in most Excel Link functions.(1.) To specify a variable name directly, enclose it in double quotes; for example, MLDeleteMatrix("Bonds").(2.).A variable-name argument without quotes is an indirect reference.The function evaluates the contents of the argument to get the variable name.The argument must be a worksheet cell address or range name.2. A data-location argument must be a worksheet cell address or range name. Do not enclose a data-location argument in quotes (except in MLGetMatrix, which has unique argument conventions).3.A data-location argument can include a worksheet number; for example, Sheet3!B1:C7 orSheet2!OUTPUT.(注意You can use virtually any special character as part of a worksheet name if you embed the sheet name within single quotes ('') when referencing it in MLGetMatrix or MLPutMatrix.) Worksheets 工作表1. After an Excel Link function successfully executes as a worksheet formula, the cell contains the value 0. While a functionis executing, the cell may continue to show the entered formula.2.We suggest selecting Move Selection after Enter on the Excel Tools Options > Edit tab. The active cell changes when an operation is complete, providing a useful confirmation for lengthy operations.3.We recommend using Excel Link functions in automatic calculation mode. If you use MLGetMatrix in manual calculation mode,enter the function in a cell, then press F9 to execute it. However, pressing F9 in this situation may also reexecute other worksheet functions and generate unpredictable results.4.To recalculate Excel Link functions in a worksheet, reexecute each function by pressing F2, then Enter.5.Pressing F9 to recalculate a worksheet affects only Excel functions (which return a value). F9 does not operate on Excel Link functions, which perform an action.6.To "automate" the recalculation of an Excel Link function, add to it some cell whose value changes. For example:=MLPutMatrix("bonds", D1:G26) + C1When the value in cell C1 changes, Excel reexecutes the MLPutMatrix function. Be careful, however, not to create endless recalculation loops.7.Excel Link functions expect A1-style worksheet cell references (columns designated with letters and rows with numbers). This is the default reference style. If your worksheet shows columns designated with numbers instead of letters, select Tools > Options andclick the General tab. Under Settings, clear the R1C1 reference style check box.8.If you use explicit cell addresses in MLGetMatrix and later insert or delete rows or columns, or move or copy the function to another cell, edit the argument to correct the addresses. Excel Link does not automatically adjust cell addresses in MLGetMatrix.9.Enter (type) Excel Link functions directly in worksheet cells.Do not use the Excel Function Wizard; it generates unpredictable results.Macros 宏1.To create macros that use Excel Link functions, you must first configure Excel to reference the functions from the Excel Link add-in. From the Visual Basic environment, select Insert > Module. When the Module page opens, select Tools > References.In the References dialog box, select the box for excllink.xla and click OK. You may have to use Browse to find the excllink.xla file.2.If you use MLGetMatrix in a macro subroutine, enter MatlabRequest on the line after MLGetMatrix. MatlabRequest initializesinternal Excel Link variables and enables MLGetMatrix to function in a subroutine. For example: Sub Get_RangeA()MLGetMatrix "A", "RangeA"MatlabRequestEnd SubDo not include MatlabRequest in a macro function unless the macro function is called from a subroutine.Data Types 数据类型Excel Link handles only MATLAB two-dimensional numeric arrays,one-dimensional character arrays (strings), and two-dimensional cell arrays.It does not work with MATLAB multidimensional arrays and structures.Dates 日期Default Excel date numbers start from January 1, 1900, while MATLAB date numbers start from January 1, 0000. Thus May 15, 1996 is 35200 in Excel and 729160 in MATLAB, a difference of 693960. If you use date numbers in MA TLAB calculations, apply the 693960 constant: add it to Excel date numbers going into MATLAB, or subtract it from MATLAB date numbers coming into Excel. If you use the optional Excel 1904 date system, the constant is 695422.Saved Worksheets Saved Worksheets 保存工作表1.When you open an Excel worksheet that contains Excel Link functions, Excel tries to execute the functions from the bottom up and right to left, thus possibly generating cell error messages (#COMMAND!, #NONEXIST!, etc.). Such behavior is usual for Excel. Simply ignore the messages, close any MATLAB figure windows, and reexecute the cell functions one at a time in the correct order by pressing F2, and then Enter.2.If you save an Excel worksheet containing Excel Link functions and later open it under a different computer environment where the excllink.xla add-in is in a different location, Excel may display a message box. Click No. Then select Edit > Links. In the Links dialogbox, click Change Source. In the Change Links dialog box, find and select excllink.xla under matlabroot/toolbox/exlink and click OK. Excel executes each function as it changes its link. You may see MATLAB figure windows and hear error beeps as the links change and functions execute; ignore them. Back in the Links dialog box, click OK. The worksheet now correctly connects to the Excel Link add-in.Or, instead of using the Edit Links menu, you can manually edit the link location in each affected worksheet cell to show the correct location of excllink.xla.Information for International Users 国际用户信息This document uses Excel with an English?(United States) Windows regional setting for illustrative purposes. If you use Excel Link with a non-English?(United?States) Windows desktop environment, certain syntactical elements may not work as illustrated. For example, you may have to replace the comma (,) delimiter within the Excel Link commands with a semicolon (;) or other operator. Please consult your Windows documentation to determine which regional setting differences exist among various international versions.matlab与excel数据交换的有利工具——excellink(4)Example 1: Regression and Curve FittingRegression techniques and curve fitting attempt to find functions that describe the relationshipamong variables. In effect, they attempt to build mathematical models of a data set. MATLAB provides many powerful yet easy-to-use matrix operators and functions to simplify the task.This example does both data regression and curve fitting. It also executes the same example in a worksheet version and a macro version. The example uses Excel worksheets to organize and display the data. Excel Link functions copy the data to MATLAB and execute MATLAB computational and graphic functions. The macro version also returns outputdata to an Excel worksheet.Worksheet VersionTo try the worksheet-only version of this example, click the Sheet1 tab on the ExliSamp.xls window.The worksheet contains one named range: A4:C28 is named DATA and contains the sample data set:1.Make E5 the active cell. Press F2, then Enter to execute the Excel Link function that copies the sample data set to MATLAB.The data set contains 25 observations of three variables. There is a strong linear dependence among the observations; in fact, they are close to being scalar multiples of each other.2.Move to cell E8 and press F2, then Enter. Repeat with cells E9 and E10. These Excel Link functions tell MATLAB to regress the third column of data on the other two columns. They createa single vector y containing the third-column data, and a new three-column matrix A consisting ofa column of ones followed by the rest of the data.3.Execute the function in cell E13. This function computes the regression coefficients by using the MATLAB backslash operation to solve the (overdetermined) system of linear equations, A*beta = y.4.Execute the function in cell E16. MATLAB matrix-vector multiplication produces the regressed result (fit).5.Execute the functions in cells E19, E20, and E21. These functions compare the original data with fit; sort the data in increasing order and apply the same permutation to fit; and create a scalar for the number of observations.6.Execute the functions in cells E24 and E25. Often it is useful to fit a polynomial equation to data. To do so, you would ordinarily have to set up a system of simultaneous linear equations and solve for the coefficients. The MA TLAB polyfit function automatesthis procedure, in this case for a fifth-degree polynomial. The polyval function then evaluates the resulting polynomial at each data point to check the goodness of fit (newfit).7.Execute the function in cell E28. The MA TLAB plot function graphs the original data (blue circles), the regressed result fit (dashed red line), and the polynomial result (solid green line); and adds a legend. Data plots.Since the data is closely correlated but not exactly linearly dependent, the fit curve (dashed line)shows a close, but not an exact,fit. The fifth-degree polynomial curve, newfit, represents a more accurate mathematical model for the data.When you have finished this version of the example, close the figure window.Macro VersionTo try the macro-and-worksheet version of this example, click the Sheet2 tab on ExliSamp.xls.Make cell A4 the active cell, but do not execute it yet.Cell A4 calls the macro CurveFit, which you can examine from the Visual Basic environment.While this module is open, select Tools > References. In the References dialog box, make sure that the excllink.xla check box is selected. If not, select the check box and click OK. You may have to use Browse to find the excllink.xla file.Back in cell A4 of Sheet2, press F2, then Enter to execute the CurveFit macro. The macro executes the same functions as in Step 1 through Step 7 of the worksheet version (in a slightly different order), including plotting the graph. Plus, it copies the original data y (sorted), the corresponding regressed data fit, and the polynomial data newfit, to the worksheet. (The last three MLGetMatrix unctions in the CurveFit macro copy data to the Excel worksheet.)When you have finished the example, close the figure window.matlab与excel数据交换的有利工具——excellink(5)插值Interpolation is a process for estimating values that lie between known data points. It is important for applications such as signal and image processing and data visualization. MATLAB provides a number of interpolation functions that let you balance the smoothness of data fit with execution speed and efficient memory use.This example uses a two-dimensional data-gridding interpolation function on thermodynamic data, where volume has been measured for time and temperature values. It finds the volume values underlying the two-dimensional time-temperature function for a new set of time and temperature coordinates.The example uses an Excel worksheet to organize and display the original data and the interpolated output data. Excel Link functions copy the data to and from MATLAB, execute the MATLAB interpolation function, and invoke MATLAB graphics to display the interpolated data in a three-dimensional color surface.To try this example, click the Sheet3 tab on ExliSamp.xls.The worksheet contains the measured thermodynamic data in cells A5:A29, B5:B29, and C5:C29. The time and temperature values for interpolation are in cells E7:E30 and F6:T6 respectively:1.Make A33 the active cell. Press F2, then Enter to execute the Excel Link function that passes the Time, Temp, and Volume labels to MATLAB.2.Make A34 the active cell. Press F2, then Enter to execute the Excel Link function that copies the original time data to MATLAB.Move to cell A35 and execute the function to copy the original temperature data. Execute the function in cell A36 to copy the original volume data.3.Move to cell A39 and press F2, then Enter to copy the interpolation time values to MATLAB. Execute the function in cell A40 tocopy the interpolation temperature values.4.Execute the function in cell A43. griddata is the MATLAB two-dimensional interpolation function that generates the interpolatedvolume data using the inverse distance method.5.Execute the functions in cells A46 and A47 to transpose the interpolated volume data and copy it to the Excel worksheet.The data fills cells F7:T30, which are enclosed in a border.6.Execute the function in cell A50. MA TLAB plots and labels the interpolated data on a three-dimensional colorsurface, with the color proportional to the interpolated volume data.When you have finished with the example, close the figure window.matlab解经典符号方程solve()solveSymbolic solution of algebraic equationsSyntaxsolve(eq)solve(eq,var)solve(eq1,eq2,...,eqn)g = solve(eq1,eq2,...,eqn,var1,var2,...,varn)DescriptionSingle Equation/Expression2The input to solve can be either symbolic expressions or strings. If eq is a symbolic expression(x^2-2*x+1) or a string that does not contain an equal sign ('x^2-2*x+1'), then solve(eq)solves the equation eq=0 for its default variable (as determined by findsym). solve(eq,var) solves the equation eq (or eq=0 in the two cases cited above) for the variablevar.System of EquationsThe inputs are either symbolic expressions or strings specifying equations. solve(eq1,eq2,...,eqn) or solves the system of equations implied by eq1,eq2,...,eqn inthe n variables determined by applying findsym to the system.g = solve(eq1,eq2,...,eqn,var1,var2,...,varn) finds the zeros for the system ofequations for the variables specified as inputs.Three different types of output are possible. For one equation and one output, the resultingsolution is returned with multiple solutions for a nonlinear equation. For a system of equations andan equal number of outputs, the results are sorted alphabetically and assigned to the outputs. Fora system of equations and a single output, a structure containing the solutions is returned.For both a single equation and a system of equations, numeric solutions are returned if symbolicsolutions cannot be determined.Examplessolve('a*x^2 + b*x + c') returns[ 1/2/a*(-b+(b^2-4*a*c)^(1/2)),1/2/a*(-b-(b^2-4*a*c)^(1/2))]solve('a*x^2 + b*x + c','b') returns-(a*x^2+c)/xS = solve('x + y = 1','x - 11*y = 5') returns a structure S withS.y = -1/3, S.x = 4/3solve :: Functions (Symbolic Math Toolbox) text://2 of 2 2012-7-26 9:A = solve('a*u^2 + v^2', 'u - v = 1', 'a^2 - 5*a + 6')returnsA =a: [4x1 sym]u: [4x1 sym]v: [4x1 sym]whereA.a =[ 2][ 2][ 3][ 3]A.u =[ 1/3+1/3*i*2^(1/2)][ 1/3-1/3*i*2^(1/2)][ 1/4+1/4*i*3^(1/2)][ 1/4-1/4*i*3^(1/2)]A.v =[ -2/3+1/3*i*2^(1/2)][ -2/3-1/3*i*2^(1/2)][ -3/4+1/4*i*3^(1/2)][ -3/4-1/4*i*3^(1/2)]关于使用solve()时产生x: [1x1 sym]的解决亲爱的同学们,你有没有在使用matlab过这样类似的情况呢?>> solve('x + y = 1','x - 11*y = 5')ans =x: [1x1 sym]y: [1x1 sym]为什么会出现这样的现象呢?这是因为你的默认输出格式是结构体,你想看的是单个的元素。
1.如何将Excel中的数据在MATLAB界面下显示出来:
首先Excel必须是office的(WPS的不可以,MATLAB不能识别),在MATLAB中使用命令xlsread读取Excel中的数据到MATLAB里,如下所示:[number,txt,raw]=xlsread('noise.xlsx'),此
时读取的文件须在MATLAB默认路径下,命令具体用法及参数含
义可直接用help命令在MATLAB中查找。
读取的数据界面如下:
2.如何将Excel中的数据导入MATLAB并在MATLAB中画出图像?
首先Excel必须是office的(WPS的不可以,MATLAB不能识
别),file-Import Data-在工作路径下找到需要导入的文件(图3)-点击打开(图4)-点击next-点击finish-导入成功-在workspace中看到图5(导入的数据)。
至此数据导入成功。
下面是如何利用该数据绘图:
法一:双击workspace中的data打开如图6,选中要画图的数据如图7(数据范围可以根据需要随便选择),点击上方的
plot(data(1:874,1:2),画出图像如图8,并且在工作空间中自动显示画图命令。
法二:直接在工作空间中写画图命令。
图3
图4
图5
图6
图7
图8。
1. 如何将Excel中的数据在MATLA界面下显示出来:首先Excel必须是office 的(WPS勺不可以,MATLA不能识别),在MATLA中使用命令xlsread读取Excel中的数据到MATLA里,如下所示:[number,txt,raw]=xlsread('noise.xlsx'),此时读取的文件须在MATLA默认路径下,命令具体用法及参数含义可直接用help 命令在MATLA中查找。
读取的数据界面如下:[0.2i60][0.24SO][0.2500][0.2520][0.2540][0.2560][0.2580][0.25Q0][0.2620][0. 2640][0. 2660][0.2680][0.2700][0.2720][0.2740][0. 2760][23.6000][ 28][30.4000]125-2000][ 18][19.2000][2L 2000][17. 20Q0][16.SOOO][17.6000][13.6000][11.2000][13.6000][13.2000][ 10][12.4000]2. 如何将Excel中的数据导入MATLA并在MATLA中画出图像?首先Excel必须是office 的(WPS勺不可以,MATLA不能识别),file-Import Data- 在工作路径下找到需要导入的文件(图3)-点击打开(图4)-点击next-点击finish- 导入成功-在workspace中看到图5 (导入的数据)。
至此数据导入成功。
下面是如何利用该数据绘图:法一:双击workspace中的data打开如图6,选中要画图的数据如图7 (数据范围可以根据需要随便选择),点击上方的plot(data(1:874,1:2),画出图像如图8,并且在工作空间中自动显示画图命令。
法二:直接在工作空间中写画图命令。
>> [number, txt raw!=zlsr*ad C noise.xlsz J)riunber 二0.246023. 60000.24SC 2& 00000.2500 30. 40000. 2520 25.20000. 2E40 18. 00000.2560 2000Di 2580 21. 20000.2600 17.20000. 2620 16.30000. 2640 17. 60000. 13. 60000* 26SA 1],20000. 2700 13. SQ00Import Data査找范圉(X):库计算机网貉名称'悸改日期,,noise.rriat2014/3/1S 19:181^gnoisenKlsx2014/3/1& 19:01I HKTIAB ▼> ®文件名曲):iriOL XISX文件类型⑴;Recognized Filww取消 |JJWorkspace圄■鱼1鲂I^Lr Select data ...〒Name *田[甌Value<874x2 double* rrrCommand Window■+* □ ? X>> plol (datadia?^ I. = 2), J DisplayMafte J p J dataCl;874? 1;2)a ? J YDataSource 1/ data(i:B74? 1:2^ ) .f igurt (ecf)九—K —图81 f 4New to MATLAB? Watch thi$ Sdg see 口旳口轧 or read Getting 5t^rt 亡cLNew Parallel D”ktq 因❼Ctirrent F file Edit 0ew Ins-ert lo&ls Desktop Window Help□ U 矗B |輿耳U 物穏摇TO □囲I ■口IXCommaHd Window。
Matlab具有强大的数据计算和处理、图形显示能力;Excel则具有强大的表格处理同时也有强大的数据统计和显示能力。
在科研过程中常会同时使用这两款软件,如果实现Matlab和Excel的链接,可以满足实际要求。
实现Matlab和Excel的混合编程主要有以下几种方法:1、利用Excel Link(最常用方法)Excel Link是一个在Windows环境下实现Excel与Matlab进行链接的插件。
通过连接Excel和Matlab,实现Excel和Matlab的数据共享。
使用Excel Link 时,不脱离Excel环境直接在Excel中调用Matlab函数。
具体可参见Matlab帮助或Excel Link手册。
2、利用Excel生成器(Matlab作COM服务器)COM(通用对象模型)是一组面向对象的技术和工具的集合。
利用Matlab提供的COM生成器,把Matlab的算法程序生成组件,这些组件可作为独立的COM对象被Excel的Visual Basic使用。
具体可参见Matlab COM Builder或者Matlab Builder for Excel工具。
3、利用Active X控制(Matlab作COM客户端)ActiveX是 Microsoft对于一系列策略性面向对象程序技术和工具的称呼,其中主要的技术是组件对象模型。
主要利用Matlab中的actxserver函数:actxserver('progid');progid为程序的ActiveX部件的标识符,针对不同的程序有不同的progid。
如actxserver('Excel.Application'),然后可利用invoke函数进行一系列操作。
4、文件导入方法利用xlsread和xlswrite函数读写Excel文件(也可用fopen、fread和fwrite函数),然后进行数据处理操作。
另外,还可以通过菜单File->Import data 方法。
专题十外部程序接口10.1 在Excel中使用MATLAB☐Spreadsheet Link程序☐将Excel表格中的数据导出到MATLAB工作空间中☐从MATLAB工作空间导入数据到Excel表格中☐在Excel中调用MATLAB函数进行运算1. Spreadsheet Link程序Spreadsheet Link程序是MATLAB提供的Excel与MATLAB的接口,通过Spreadsheet Link,可以在Excel工作区和MATLAB工作区之间进行数据交换,也可以在Excel中调用MATLAB的函数。
(1)安装Spreadsheet Link的安装是在MATLAB安装过程中,随其他组件一起安装的。
安装完成后,还需要在Excel中进行一些设置后才能使用。
(2)设置选 Excel “文件”菜单的“选项”菜单项,打开“Excel选项”对话框。
在“Excel选项”对话框中,选左边栏的“加载项”,然后单击右边面板下部的“转到”按钮,弹出“加载宏”对话框。
(2)设置在“加载宏”对话框中,单击“浏览”按钮,打开文件“浏览”对话框。
在文件“浏览”对话框,选MATLAB的安装文件夹的子文件夹toolbox\exlink。
在文件夹exlink中选excllink.xlam文件,单击“确定”按钮返回。
(2)设置“加载宏”对话框的“可用加载宏”列表中多了一个“Spreadsheet Link 3.2.5 for use with MATLAB and Excel”选项。
选中该项,返回Excel窗口。
(3)MATLAB命令组加载Spreadsheet Link 后,在Excel窗口“开始”选项卡中多了一个MATLAB命令组。
命令组中包含的一组命令项,分别用于实现启动MATLAB、导入和导出数据、调用MATLAB函数等。
2. 将Excel表格中的数据导出到MATLAB工作空间例1 文件“测试数据.xlsx”存储某门店销售数据,将其中的A~E列的数据导出MATLAB工作区,存入变量sale。
ii. 设置Excel Link的自启动
通过"MATLAB"-->"选项(Preferences)"可以打开选项配置窗口.如下图所示,
如果不想MATLAB每次在Excel启动时都自动启动,请取消第一个选项,当然也可以通过在数据单元格中输入命令的方式来实现关闭MATLAB的自启动,方法是在Excel的任意一个单元格中输入下面命令:=MLAutoStart("no"),然后回车就行了,要注意不要忘了前面有个等号!!!这该单元格显示的结果是0,也就是说在下次启动时,MATLAB不会随Excel的启动而启动了.该函数将会改变Excel初始化文件中对自启动Excel Link和MATLAB的设置.
3. Excel Link的链接管理函数
在上面我们已经用到了函数MLAutoStart(),现在讲解下几个函数的用法:
MLAutoStart() 设置是否自动启动MATLAB,参数可以是"no"或者"yes"
MATLABinit() 初始化Excel Link,并且启动MATLAB
MLClose() 关闭MATLAB进程
MLOpen() 启动MATLAB进程
在上面的命令中,MATLABinit只能以宏命令的方式运行,而其它的3个命令可以作为数据单元函数或者宏命令来执行.宏命令的运行方式是"开发工具"-->"宏"-->输入宏名-->"执行".
4. Excel Link的数据管理函数
Excel提供了9个数据管理函数,实现MATLAB与Excel之间的数据复制,并可在Excel中执行MATLAB命令.
MATLABfcn() 对于给定的Excel数据,运行MATLAB命令
MATLABsub() 对于给定的Excel数据,运行MATLAB命令并指定输出位置MLDeleteMatrix() 删除MATLAB矩阵
MLEvalString() 执行MATLAB命令
MLGetMatrix() 向Excel数据表中写入MATLAB矩阵的数据内容
MLGetVar() 向Excel数据表VBA写入MATLAB矩阵的数据内容MLAppendMatrix() 向MATLAB空间添加Excel数据表的数据
MLPutMatrix() 向Excel数据表创建或覆盖MATLAB矩阵
MLPutVar() 向Excel数据表VBA创建或覆盖MATLAB矩阵
值得注意的是,我们能以数据单元函数的形式或者宏命令的形式调用除MLGetVar()和MLPutVar()以外所有的数据处理函数,而MLGetVar()和MLPutVar()函数只能以宏命令的形式被调用.。