## 从 Python 对 MATLAB 数据进行分类并绘图

```import matlab.engine eng = matlab.engine.start_matlab() eng.eval("T = readtable('patients.dat');",nargout=0) ```

MATLAB `readtable` 函数将数据读入表中。引擎不支持 MATLAB 表数据类型。不过，使用 MATLAB `table2struct` 函数，您可以将表转换为标量结构体，这是引擎支持的数据类型。

```eng.eval("S = table2struct(T,'ToScalar',true);",nargout=0) eng.eval("disp(S)",nargout=0) ```
``` LastName: {100x1 cell} Gender: {100x1 cell} Age: [100x1 double] Location: {100x1 cell} Height: [100x1 double] Weight: [100x1 double] Smoker: [100x1 double] Systolic: [100x1 double] Diastolic: [100x1 double] SelfAssessedHealthStatus: {100x1 cell} ```

```D = eng.workspace["S"] ```

`S` 的字段包含数组。该引擎将元胞数组转换为 Python `list` 变量，并将数值数组转换为 MATLAB 数组。因此，`D["LastName"]` 的数据类型为 `list`，而 `D["Age"]` 的数据类型为 `matlab.double`

`smoker = matlab.logical(D["Smoker"])`

`Diastolic` 血压读数和 `Smoker` 指示符转换为 1×100 MATLAB 数组以进行分类。

```pressure = D["Diastolic"] pressure.reshape((1,100)) pressure = pressure[0] smoker.reshape((1,100)) smoker = smoker[0] ```

`pressure` 数组分为吸烟者和非吸烟者的血压读数列表。Python 列表推导式为迭代序列提供了一种紧凑的方法。使用 Python `zip` 函数，可以在单个 `for` 循环中迭代多个序列。

```sp = [p for (p,s) in zip(pressure,smoker) if s is True] nsp = [p for (p,s) in zip(pressure,smoker) if s is False] ```

```print(len(sp)) ```
```34 ```

```print(len(nsp)) ```
```66 ```

```sp = matlab.double(sp) nsp = matlab.double(nsp) print(eng.mean(sp)) ```
```89.9117647059 ```

```print(eng.mean(nsp)) ```
```79.3787878788 ```

```sdx = eng.linspace(1.0,34.0,34) nsdx = eng.linspace(1.0,34.0,66) ```

```eng.figure(nargout=0) eng.hold("on",nargout=0) eng.box("on",nargout=0) ```

```eng.scatter(sdx,sp,10,'blue') ```
```<matlab.object object at 0x22d1510> ```

```h = eng.scatter(nsdx,nsp,10,'red') h = eng.xlabel("Patient (Anonymized)") h = eng.ylabel("Diastolic Blood Pressure (mm Hg)") h = eng.title("Blood Pressure Readings for All Patients") h = eng.legend("Smokers","Nonsmokers") ```

```x = matlab.double([0,35]) y = matlab.double([89.9,89.9]) h = eng.line(x,y,"Color","blue") h = eng.text(21.0,88.5,"89.9 (Smoker avg.)","Color","blue") y = matlab.double([79.4,79.4]) h = eng.line(x,y,"Color","red") h = eng.text(5.0,81.0,"79.4 (Nonsmoker avg.)","Color","red") ```