UnityとMatlabを連携する

ここを参考に
qiita.com


MatlabにInstrument toolboxが必要(tcpipメソッド)

  result = num2str('111');
  tcpipClient = tcpip('127.0.0.1',55001,'NetworkRole','Client');
  set(tcpipClient,'Timeout',30);
  fopen(tcpipClient);
  fwrite(tcpipClient,result);
  fclose(tcpipClient);

Unity

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 paramRead : MonoBehaviour
{
    TcpListener listener;
    String msg;

    // Start is called before the first frame update
    void Start()
    {
        listener = new TcpListener(55001);
        listener.Start();
        print("is listening");
    }

    // Update is called once per frame
    void Update()
    {
        if (!listener.Pending())
        {
        }
        else
        {
            TcpClient client = listener.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            StreamReader reader = new StreamReader(ns);
            msg = reader.ReadToEnd();
            print(msg);
        }
    }
}