To communicate between WSL2 ROS and MATLAB/Simulink, you would need to make sure the environment variable "ROS_IP" being set correctly on both MATLAB and WSL2 sides. Otherwise, it might ended up getting only part of working functionalities. Below are steps to get this work. Feel free to skip directly to Step 3 if you already have WSL2 enabled and ROS Noetic installed on WSL2.
Step 1: Enable WSL2 on Windows
You may follow this offical link to install WSL2 on windows, I would recommand using Ubuntu20.04 and ROS Noetic if you would like to use Gazebo in your project later on.
Step 2: Install ROS/ROS 2 on WSL2
Step 3: Configure Environment Variables
To make sure specific URI is known for both side of ROS (ROS NetworkSetup wiki), you can either use the automatic configuration function or set it up by yourself.
1. Type in "$ifconfig" on WSL2 Terminal, look for "inet" under "eth0". This will be your WSL2 IP Address.
2. Download configureWSL2.m for attachment and launch MATLAB.
3. Call "configureWSL2" with IP Address, Username, Password of your WSL2 as input. This function will setup all required environment variables for you. Note that it assumes you have installed ROS Noetic on Ubuntu20.04. If you installed different version of ROS or installed it in another directory other than the default directory, you may need to modify the "ROSPath" variable in the function.
Manually set it up:
1. Type in "$ifconfig" on WSL2 Terminal, look for "inet" under "eth0". This will be your WSL2 IP Address. Set this on WSL2 side (e.g. $ export ROS_IP=xxx.xxx.xxx.xxx)
2. Type in ">ipconfig" on Windows Command Prompt and find IPv4 Address under "Ethernet adapter vEthernet (WSL)". Set this on MATLAB side (e.g. >> setenv('ROS_IP','xxx.xxx.xxx.xxx'))
Step 4: Visualization with Gazebo and Rviz
To run GUI applications such as Gazebo and Rviz and bring them to your windows desktop, you need to install X server on Windows, such as VcXsrc.
If you already run "configureWSL2.m" in Step 3, all you need to do is launch "Xlaunch" (VcXsrv) and type in "$gazebo" on WSL2 terminal.
As an alternative, you may also set environment variables manually referring to this video.
Hope this helps,
MathWorks Robotics and Autonomous Systems Team