Skip to main content

MATLAB 中的现代电子表格

MATLAB 是一个数值计算平台。它具有原生 table 类型,对电子表格的支持有限。

¥MATLAB is a numeric computing platform. It has a native table type with limited support for spreadsheets.

SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。

¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.

该演示使用 SheetJS 从电子表格中提取数据,以便在 MATLAB 中进行进一步分析。我们将探讨如何运行外部工具将复杂的电子表格转换为 MATLAB 的简单 XLSX 文件。

¥This demo uses SheetJS to pull data from a spreadsheet for further analysis within MATLAB. We'll explore how to run an external tool to convert complex spreadsheets into simple XLSX files for MATLAB.

测试部署

此演示由 SheetJS 用户在以下部署中进行了测试:

¥This demo was tested by SheetJS users in the following deployments:

架构版本日期
darwin-x64R2024a2024-06-09
win10-x64R2024a2024-06-09

MATLAB 对通过 readtable[^1] 和 writetable[^2] 处理电子表格的支持有限。在撰写本文时,它缺乏对 XLSB、NUMBERS 和其他常见电子表格格式的支持。

¥MATLAB has limited support for processing spreadsheets through readtable[^1] and writetable[^2]. At the time of writing, it lacked support for XLSB, NUMBERS, and other common spreadsheet formats.

SheetJS 库通过将电子表格标准化为 MATLAB 可以理解的形式来帮助填补这一空白。

¥SheetJS libraries help fill the gap by normalizing spreadsheets to a form that MATLAB can understand.

集成详情

¥Integration Details

MATLAB 目前不提供解析 CSV 字符串或表示文件数据的字符数组的方法。readtablewritetablecsvreadcsvwrite 直接使用文件系统。strreadtextscan 是专门为读取数字而设计的。

¥MATLAB does not currently provide a way to parse a CSV string or a character array representing file data. readtable, writetable, csvread, and csvwrite work with the file system directly. strread and textscan are designed specifically for reading numbers.

当前的建议涉及一个专用的命令行工具,该工具利用 SheetJS 库来执行电子表格处理。

¥The current recommendation involves a dedicated command-line tool that leverages SheetJS libraries to to perform spreadsheet processing.

SheetJS NodeJS 模块 可以在 NodeJS 脚本中加载并打包在独立的命令行工具中。

¥The SheetJS NodeJS module can be loaded in NodeJS scripts and bundled in standalone command-line tools.

命令行工具

¥Command-Line Tools

"命令行工具" 演示 创建 xlsx-cli,这是一个读取电子表格文件并生成输出的命令行工具。"NodeJS" 部分中的示例能够使用 --xlsx 命令行标志生成 XLSX 电子表格:

¥The "Command-Line Tools" demo creates xlsx-cli, a command-line tool that reads a spreadsheet file and generates output. The examples in the "NodeJS" section are able to generate XLSX spreadsheets using the --xlsx command line flag:

$ xlsx-cli --xlsx ./pres.numbers ## generates pres.numbers.xlsx

该命令行工具支持多种格式,包括 XLSB (--xlsb)。

¥The command-line tool supports a number of formats including XLSB (--xlsb).

这些工具将 SheetJS readFile[^3] 和 writeFile[^4] 方法配对,从任意电子表格文件中读取数据并转换为 XLSX:

¥The tools pair the SheetJS readFile[^3] and writeFile[^4] methods to read data from arbitrary spreadsheet files and convert to XLSX:

const XLSX = require("xlsx"); // load the SheetJS library
const wb = XLSX.readFile("input.xlsb"); // read input.xlsb
XLSX.writeFile(wb, "output.xlsx"); // export to output.xlsx

MATLAB 命令

¥MATLAB commands

MATLAB system command[^5] 可以运行 M 文件中的命令行工具。例如,如果 xlsx-cli 工具放置在工作区文件夹中,测试文件 pres.numbers 位于 Downloads 文件夹中,则以下命令会生成 XLSX 文件 pres.numbers.xlsx

¥The MATLAB system command[^5] can run command-line tools in M-files. For example, if the xlsx-cli tool is placed in the workspace folder and the test file pres.numbers is in the Downloads folder, the following command generates the XLSX file pres.numbers.xlsx :

% generate ~/Downloads/pres.numbers.xlsx from ~/Downloads/pres.numbers
system("./xlsx-cli --xlsx ~/Downloads/pres.numbers");

在交互式会话中,可以使用感叹号运算符 ![^6]:

¥In an interactive session, the exclamation point operator ![^6] can be used:

% generate ~/Downloads/pres.numbers.xlsx from ~/Downloads/pres.numbers
!./xlsx-cli --xlsx ~/Downloads/pres.numbers

读取文件

¥Reading Files

从任意电子表格开始,xlsx-cli 可以生成 XLSX 工作簿。工作簿写入后,可以使用 readtable 解析 XLSX 文件:

¥Starting from an arbitrary spreadsheet, xlsx-cli can generate a XLSX workbook. Once the workbook is written, the XLSX file can be parsed with readtable:

% `filename` points to the file to be parsed
filename = "~/Downloads/pres.numbers";
% generate filename+".xlsx"
system("./xlsx-cli --xlsx " + filename)
% read using `readtable`
tbl = readtable(filename + ".xlsx");

下图描绘了练习册华尔兹:

¥The following diagram depicts the workbook waltz:

写入文件

¥Write Files

从 MATLAB 表开始,writetable 可以生成 XLSX 工作簿。工作簿编写完成后,xlsx-cli 可以转换为 NUMBERS 或其他格式:

¥Starting from an MATLAB table, writetable can generate a XLSX workbook. Once the workbook is written, xlsx-cli can translate to NUMBERS or other formats:

% tbl is the table
tbl = table({"Sheet";"JS"}, [72;62], 'VariableNames', ["Name", "Index"])
% `filename` points to the file to be written
filename = "~/Downloads/sorted.xlsx";
% write using `writetable`
writetable(tbl, filename);
% generate filename+".xlsb"
system("./xlsx-cli --xlsb " + filename);

下图描绘了练习册华尔兹:

¥The following diagram depicts the workbook waltz:

完整演示

¥Complete Demo

该演示在 macOS 上进行了测试。路径名称在其他平台上会有所不同。

¥This demo was tested in macOS. The path names will differ in other platforms.

该演示处理 pres.numbers。该演示分为 3 个部分:

¥This demo processes pres.numbers. There are 3 parts to the demo:

A)"导入":SheetJS 工具将读取测试文件并生成一个干净的 XLSX 文件。MATLAB 将使用 readtable 读取该文件。

¥A) "Import": SheetJS tooling will read the test file and generate a clean XLSX file. MATLAB will read the file using readtable.

B)"过程":使用 sortrows,MATLAB 将反转表顺序。

¥B) "Process": Using sortrows, MATLAB will reverse the table order.

C)"导出":修改后的表将使用 writetable 导出到 XLSX。SheetJS 工具会将文件转换为 XLSB。

¥C) "Export": The modified table will be exported to XLSX using writetable. SheetJS tooling will convert the file to XLSB.

  1. 创建独立的 xlsx-cli 二进制文件 [^7]:

    ¥Create the standalone xlsx-cli binary[^7]:

cd /tmp
npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz exit-on-epipe commander@2
curl -LO https://xlsx.nodejs.cn/cli/xlsx-cli.js
npx nexe -t 14.15.3 xlsx-cli.js
  1. 将生成的 xlsx-cli 移动到 MATLAB 工作区文件夹。在 macOS 上,此文件夹通常是 ~/Documents/MATLAB/

    ¥Move the generated xlsx-cli to the MATLAB workspace folder. On macOS, this folder is typically ~/Documents/MATLAB/:

mkdir -p ~/Documents/MATLAB/
mv xlsx-cli ~/Documents/MATLAB/
  1. 下载 https://xlsx.nodejs.cn/pres.numbers 并保存到下载文件夹:

    ¥Download https://xlsx.nodejs.cn/pres.numbers and save to Downloads folder:

cd ~/Downloads/
curl -LO https://xlsx.nodejs.cn/pres.numbers
  1. 将以下内容保存到工作区文件夹中的 SheetJSMATLAB.m

    ¥Save the following to SheetJSMATLAB.m in the workspace folder:

SheetJSMATLAB.m
% Import data from NUMBERS file
system("./xlsx-cli --xlsx ~/Downloads/pres.numbers");
tbl = readtable("~/Downloads/pres.numbers.xlsx");
% Process data (reverse sort)
sorted = sortrows(tbl,"Index", "descend");
% Export data to XLSB workbook
writetable(sorted,"~/Downloads/sorted.xlsx");
system("./xlsx-cli --xlsb ~/Downloads/sorted.xlsx");
  1. 在 MATLAB 桌面会话中,运行 SheetJSMATLAB 命令:

    ¥In a MATLAB desktop session, run the SheetJSMATLAB command:

>> SheetJSMATLAB

它将在 ~/Downloads 文件夹中创建文件 sorted.xlsx.xlsb。打开文件,确认表是按照 Index 降序排列的:

¥It will create the file sorted.xlsx.xlsb in the ~/Downloads folder. Open the file and confirm that the table is sorted by Index in descending order:

Name           Index
Joseph Biden 46
Donald Trump 45
Barack Obama 44
GeorgeW Bush 43
Bill Clinton 42

如果 matlab 命令在系统 PATH 上可用,则该命令的 "headless" 版本为:

¥If the matlab command is available on the system PATH, the "headless" version of the command is:

cd ~/Documents/MATLAB
matlab -batch SheetJSMATLAB

[^1]: 请参阅 MATLAB 文档中的 readtable

¥See readtable in the MATLAB documentation.

[^2]: 请参阅 MATLAB 文档中的 writetable

¥See writetable in the MATLAB documentation.

[^3]: 见 readFile 于 "读取文件"

¥See readFile in "Reading Files"

[^4]: 见 writeFile 于 "写入文件"

¥See writeFile in "Writing Files"

[^5]: 请参阅 MATLAB 文档中的 system

¥See system in the MATLAB documentation.

[^6]: 请参阅 MATLAB 文档中的 “MATLAB 运算符和特殊字符

¥See "MATLAB Operators and Special Characters in the MATLAB documentation.

[^7]: 详细信息请参见 "命令行工具"

¥See "Command-line Tools" for more details.