# MATLAB’s users

CLASSIX’s provide easy-to-use Maltab package classix-matlab. Besides, you can easily setup call Python’s CLASSIX in Matlab.

We will provide a simple demostration for Matlab’s users to call CLASSIX from Matlab.

Note

This example is provided by Mike Croucher, one can access the original scripts of live code format (.mlx) and jupyter notebook file (.ipynb) in CLASSIX’s GitHub repository. For more details of using Python’s package in Matlab and MATLAB kernel for Jupyter released, feel free to visit Mike’s blog for answers.

## Enviroment setup

First, ensure you reference Python (cf. https://www.python.org) and Python’s CLASSIX is properly presented (cf. Installation guide)

Then, you can check your Python enviroment in your Matlab using:

```
pe = pyenv()
```

In Windows platform, it might show the below information:

pe = PythonEnvironment with properties: Version: "3.11" Executable: "c:Users...AppDataLocalProgramsPythonPython311python.exe" Library: "c:Users...AppDataLocalProgramsPythonPython311python311.dll" Home: "c:Users...AppDataLocalProgramsPythonPython311" Status: NotLoaded ExecutionMode: InProcess

Otherwise, you can setup enviroment in your Matlab via below command:

```
pe = pyenv(Version="C:\Users\cclcq\AppData\Local\Programs\Python\Python311\python.exe")
```

All Python commands must be prefixed with py. So, to compute the Python command math.sqrt(42) we simply do:

```
py.math.sqrt(42)
```

## Clustering analysis

After ensuring everying is presented, we can performing a basic clustering analysis on MATLAB data using CLASSIX as below:

Let’s start by generating and plotting some data using MATLAB.

```
mu1 = [2 2]; % Mean of the 1st cluster
sigma1 = [2 0; 0 1]; % Covariance of the 1st cluster
mu2 = [-4 -3]; % Mean of the 2nd cluster
sigma2 = [1 0; 0 1]; % Covariance of the 2nd cluster
r1 = mvnrnd(mu1,sigma1,100);
r2 = mvnrnd(mu2,sigma2,100);
X = [r1; r2];
```

Calling CLASSIX is straightforward. We don’t even need to convert the MATLAB array X to a Numpy array as it’s all done automatically.

```
rng('default') % For reproducibility
plot(X(:,1),X(:,2),"*",MarkerSize=5);
clx = py.classix.CLASSIX(radius=0.3, verbose=0);
clx = clx.fit(X);
clx.explain(plot=false);
```

The cluster labels of each data point are available in clx.labels_. This is a Numpy array:

```
class(clx.labels_)
```

but no conversion is required when using this in the MATLAB scatter command:

```
scatter(X(:,1),X(:,2),10,clx.labels_,"filled");
```

## Explainability and plotting

A key feature of CLASSIX is that it can provide textual explanations of the computed clustering results, making it a fully explainable clustering algorithm. The CLASSIX `explain()`

method can also produce plots, but you may receive an error message when attempting to do this from MATLAB:

```
clx.explain(plot=true)
```

`explain()`

method requires MATLAB TCL installed, this is explained on MATLAB Answers at Why am I not able to call python Tkinter in MATLAB? - MATLAB Answers - MATLAB Central (mathworks.com). We need to provide paths to TCL.

To setup the enviroment, use:

```
setenv('TCL_LIBRARY', 'C:\Program Files\Python311\tcl\tcl8.6')
setenv('TK_LIBRARY', 'C:\Program Files\Python311\tcl\tk8.6')
clx.explain(plot=true)
```

Note

One need to find the correct paths on your machine for MATLAB TCL.