Matlab and Unity connection

조회 수: 52 (최근 30일)
Si Yi
Si Yi 2023년 2월 3일
댓글: Si Yi 2023년 3월 26일
I am currently working on a project that requires measuring objects in real-time, and developing it into an application using Unity. I would like to know if there is a connection between MatLab and Unity, that enables me to do so.

답변 (1개)

HimeshNayak
HimeshNayak 2023년 2월 13일
Hi Si Yi,
I understand that you want to create a connection between MATLAB and unity to develop an application in Unity.
According to my research, I have come up with the following steps which may help you to connect MATLAB and Unity. We can connect them with the help of TCP.
1) Firstly, in Unity set up a TcpListener that can accept the commands at a particular IP Address and port to which MATLAB will be connected.
The following C# code listens to IP Address 127.0.0.1 with port 55001.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System;
using System.IO;
using System.Text;
public class DemoObject : MonoBehaviour {
   // initialize the communication with MatLab    
   TcpListener listener;
  
void Start() {
   // LISTEN TO MATLAB. Set up unity listening to matlab
       listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 55001);
       listener.Start();
       print("is listening");
   }
}
2) After executing the code. Run the following MATLAB code to connect it to the above-mentioned IP and port so that the commands entered in MATLAB can be reflected in Unity.
% set up the code for communicating with Unity
tcpipClient = tcpclient('127.0.0.1',55001);
set(tcpipClient,'Timeout', 3);
Now the connection has been set up and we can write more code to perform actions such as move an object using arrow keys as shown in the following video.
Regards,
Himesh
  댓글 수: 1
Si Yi
Si Yi 2023년 3월 26일
Hello Himesh,
Thanks for the help. However, I am unable to established the connection. I followed the Youtube video, and did not get any errors or issues. (I also installed PsychToolKit in my MATLAB) But when I attempt to press 'Up' or 'Down', I am unable to command the cylinder in Unity to do so. I believe it is because the handshake is not established. I've also double checked my IP address and port, as well as my firewall is turned off. Unfortunately, I am still unable to get the connection, why is that so?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by