Detailed explanation of matlab reading excel files

MATLAB is a very powerful scientific computing software that can not only perform numerical calculations and matrix operations, but also process data in Excel, CSV and other common formats. In real life, Excel files are very common data files, which contain various information, such as data records, calculations, and forecasts. MATLAB provides a set of tools to easily read in and process Excel files. This article will introduce how to use MATLAB to read Excel files.

  1. Prepare Excel file

First, we need an Excel file to read. We can use the Excel software to create an Excel file containing the data and save it to the computer, or we can use MATLAB's "xlsread" function to read an existing Excel file. Here's a basic xlsreadsyntax:

[num,txt,raw] = xlsread(filename);

Among them, "filename" is the name of the Excel file to be read, and "num", "txt" and "raw" returned by the function are arrays of data and text. When we do not specify the output parameter, "xlsread" will return the array "num" by default, which contains the numbers in the Excel file. Please note that if the Excel table contains text or other non-numeric information, it will not be able to read the information, you need to use txtor rawoutput parameters to get the relevant data.

  1. read numeric data

Reading Excel numeric data is easy, just use the "xlsread" function. The following is a common example. In the Excel file "example.xlsx", there are two columns of data, "Column A" and "Column B", which are numbers from 1 to 10:

[num,txt,~] = xlsread('example.xlsx');
columnA = num(:,1);
columnB = num(:,2);

The code first reads all the data of the "example.xlsx" file using the "xlsread" function, and then stores the data of the first and second columns into the MATLAB variables "columnA" and "columnB", respectively.

  1. read text data

If the Excel table contains text data, you need to use txtoutput parameters to extract the relevant data. For example, suppose we want to read the string contained in the first column, we need to add the "txt" parameter to the "xlsread" function:

[num,txt,~] = xlsread('example.xlsx');
columnA_txt = txt(2:end,1);

This code reads all the data in the "example.xlsx" file and gets the string of the first column. Note that we use "2:end" to skip the first row since the first row in Excel usually contains headers rather than actual data.

  1. read formula data

Excel is also capable of containing formulas rather than fixed numbers or text. Formulas contain calculations that are applied to a specific cell or range of cells. MATLAB's "xlsread" function is able to evaluate these formulas as numeric values ​​and read them into MATLAB. The following is an example, where the Excel file "example.xlsx" contains two columns of data, the first column is the number from 1 to 10, and the second column is the square of the number in the first column and the multiple of two times:

[num,~,~] = xlsread('example.xlsx');
columnC = num(:,3);

The third column of the Excel file contains a calculation formula that will use the numbers in the first column for the calculation. xlsreadThe function automatically calculates the formula and stores the result in the "num" array in the third column.

  1. read multiple tables

Among them, an Excel file can contain multiple tables, and any table can be read by using the table name or table index. Here is an example where the Excel file "example.xlsx" contains 3 different sheets "Sheet1", "Sheet2" and "Sheet3":

[num_sheet1,~,~] = xlsread('example.xlsx', 'Sheet1');
[num_sheet2,~,~] = xlsread('example.xlsx', 2);  % 使用表格索引
[num_sheet3,~,~] = xlsread('example.xlsx', 'Sheet3');

The code in line 1 reads all the numeric data from the sheet named "Sheet1" and stores it in the "num_sheet1" array. Lines 2 and 4 use the table index to get the data in "Sheet2" and "Sheet3". In this example, "Sheet2" is the second sheet, so use the number "2" instead of the sheet name.

When reading Excel files, you must ensure that both MATLAB and Excel software are closed, because Excel files cannot be read or modified by two programs at the same time.

In summary, MATLAB provides a fast and flexible way to read and process data from Excel files. This process involves accessing multiple attributes such as numbers, text, formulas, and multiple tables in the Excel file, which can maximize the use of data and functions in Excel and provide great convenience in the data analysis process.

Guess you like

Origin blog.csdn.net/weixin_44463965/article/details/130482930