Main Content

Read Spreadsheet Data Using Import Tool

此示例显示如何使用导入工具将来自电子表格中的数据导入工作区,也可以从剪贴板导入数据。

选择Data Interactively

在这方面tab, in theVariable部分,点击进口数据。或者,在当前文件夹浏览器中,双击具有扩展名的文件的名称.xl..xlsx..xl.b, 或者.xl.M.。导入工具打开。

选择the data you want to import. For example, the data in the following figure corresponds to data for three column vectors. You can edit the variable name within the tab, and you can select noncontiguous sections of data for the same variable.

在这方面进口tab, in the输出类型section, select how you want the data to be imported. The option you select dictates the data type of the imported data.

选项选项 How Data Is Imported
列向量 进口each column of the selected data as an individualM.-by-1矢量。
数字矩阵 将所选数据导入M.-by-N.数字数组。
字符串数组 将所选数据导入M.-by-N.字符串数组。
细胞阵列 将所选数据导入可包含多种数据类型的单元数组,例如数字数据和文本。
Table 进口selected data as a table.

如果你选择一个矩阵或导入数据N.umeric column vectors, the tool highlights any nonnumeric data in the worksheet. Each highlight color corresponds to a proposed rule to make the data fit into a numeric array. For example, you can replace nonnumeric values with。此外,您可以在将光标放在各个单元格上时,您可以看到如何导入数据。

您可以添加,删除,重新排序或编辑规则,例如更改替换值到另一个价值。所有规则仅适用于导入的数据,也不适用于文件中的数据。在任何时间包括非数字数据的时间指定规则,并且您正在导入矩阵或数字列向量。

任何包含的细胞#错误?correspond to formula errors in your spreadsheet file, such as division by zero. The Import Tool regards these cells as nonnumeric.

当你点击时进口选择按钮那the Import Tool creates variables in your workspace.

有关与Import工具进行交互的更多信息,请注意这一点视频

进口数据from Multiple Spreadsheets

如果计划在多个文件上执行相同的导入操作,则可以从“导入”工具生成代码,使其更轻松地重复操作。在所有平台上,导入工具可以生成可以编辑和运行以导入文件的程序脚本。关于微软®Windows®systems with Excel®软件,导入工具可以生成可以为每个文件调用的函数。

例如,假设您在命名的当前文件夹中有一组电子表格M.yfile01.xlsx通过myfile25.xlsx.,并且您想要导入相同的数据范围,A2:G100.,来自每个文件的第一个工作表。生成代码以导入整套文件,如下所示:

  1. 在导入工具中打开其中一个文件。

  2. 来自进口选择按钮,选择生成函数。导入工具生成类似于以下摘录的代码,并在编辑器中打开代码。

    函数数据= ImportFile(WorkbookFile,SheetName,Range)%ImportFile从电子表格导入数字数据...
  3. Save the function.

  4. 在单独的程序文件或命令行中,创建一个为了loop to import data from each spreadsheet into a cell array namedM.yData

    numfiles = 25;范围='A2:G100';薄片= 1;mydata = cell(1,numfiles);for filenum = 1:numfiles filename = sprintf('myfile%02d.xlsx',filenum);mydata {filenum} = importfile(文件名,工作表,范围);结尾

每个细胞M.yDatacontains an array of data from the corresponding worksheet. For example,mydata {1}包含来自第一个文件的数据,M.yfile01.xlsx

粘贴Data from Clipboard

除了以交互方式导入数据外,还可以将电子表格数据从剪贴板粘贴到MATLAB中®

首先,选择并复制电子表格数据Microsoft Excel然后使用以下方法之一:

  • 在Workspace浏览器标题栏上,单击那and then select粘贴

  • 在变量编辑器中打开现有变量,右键单击,然后选择粘贴Excel Data

  • 称呼Uiimport -PasteSpecial.

也可以看看

||||

Related Topics