Exchange Data Between MATLAB and Excel
Exchange data between MATLAB® and Microsoft® Excel® and display the results back into Excel. We create a diagonal matrix using the the Spreadsheet Link add-in or using a VBA code and, finally, using MATLAB function wizard, which helps you learn how to use the MATLAB sub function.
Published: 9 Oct 2019
In this video I am going to show you how to exchange data between MATLAB and MS Excel in three different ways through an example.
- Excel Ribbon
- VBA macro
- MATLAB Function Wizard
In a worksheet, enter the numbers 1 through 5 into the range of cells from A1 through E1.
Define the name testData for this range of cells.
On the Home tab, click the MATLAB group then select Send named ranges to MATLAB.
Select Run MATLAB Command.
Use the diag function to specify testData as the input argument. d = diag(testData)
Select Get data from MATLAB.
Enter d in the dialog box to get the diagonal matrix.
Spreadsheet Link allows you to execute MATLAB functions in VBA if you use macros to automate your data-processing calculation.
In the Code section, enter this VBA code that contains a macro named Diagonal.
Sub Diagonal()
MLPutRanges
MLEvalString "b = diag(testData);"
MLGetMatrix "b", "A3"
MatlabRequest
End Sub
This code sends the named ranges to MATLAB, creates the diagonal matrix, and places it at cell A3.
Run the macro by clicking Run, Run Sub/UserForm.
You can select MATLAB function wizard from the MATLAB group.
In the MATLAB command window, locate the diag function by entering which diag. Choose the category matlab\elmat and function diag.
Specify testData as the input argument of V and the output cell A3. The diagonal matrix is placed at cell A3.
By double-clicking on the cell where it shows zero, you can learn the syntax of matlabsub function.
To recap, we have seen in this video how exchange and manipulate data in MATLAB from Excel in three different ways. To find out more about the capabilities of Spreadsheet Link, please visit the Spreadsheet Link product page, where you can read the documentations, watch additional videos, and find examples. Thank you for watching.