简体   繁体   English

我的Java程序如何能够连续使用MATLAB,而不必每次都建立连接?

[英]How can my Java program use MATLAB continuously without having to establish a connection everytime?

I have a MATLAB script running on a Java Eclipse project via the matlabcontrol.jar package. 我有一个通过matlabcontrol.jar包在Java Eclipse项目上运行的MATLAB脚本。

I have the following set-up 我有以下设置

MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
            .setUsePreviouslyControlledSession(true)
            .setHidden(true)
            .setMatlabLocation(null).build();

MatlabProxyFactory factory = new MatlabProxyFactory(options);

MatlabProxy proxy = factory.getProxy();

//some code invoking the proxy.eval() method

The problem is that I have the MATLAB script running several times over the course of the runtime of the simulation. 问题是我在仿真的运行过程中使MATLAB脚本多次运行。 How can I make it so that Java doesn't have to reconnect with MATLAB every single time I want to use the MATLAB function? 如何使Java不必每次都想使用MATLAB函数重新与MATLAB连接?

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks! 谢谢!

I'm not expert on JAVA, so I will give my solution in simple words that you can translate to JAVA world. 我不是JAVA的专家,所以我将用简单的词来介绍我的解决方案,您可以将其翻译为JAVA world。 :) :)

  1. Create a JAVA singleton class which is responsible for handling connections. 创建一个JAVA单例类,该类负责处理连接。
  2. Provide a public static read-only property pointing to singleton object. 提供指向单例对象的公共静态只读属性。
  3. Now use singleton object to call MATLAB functions. 现在使用单例对象调用MATLAB函数。

Eg: 例如:

public class MATLABConnector
{
      private MATLABConnector con=new MATLABConnector();
      MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
        .setUsePreviouslyControlledSession(true)
        .setHidden(true)
        .setMatlabLocation(null).build();

      MatlabProxyFactory factory = new MatlabProxyFactory(options);

      private MATLABConnector() 
      {
            // Do basic initializations.
      }        

      private boolean checkConnecionStatus();
      private boolean establishConnection();
      public static MATLABProxy getProxy()
         {
              if(!con.checkConnectionStatus())
                    con.establishConnection();
              return con.factory.getProxy();
         }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用IDE /外部工具的情况下在Java程序和数据库之间建立连接? - How to establish connection between Java Program and Database without using IDE/ External Tools? 如何建立从我的 java 程序到 sqlite3 的连接? 尝试时包错误 - How do I establish a connection from my java program to sqlite3? Package error when attempting Java程序可以通过代理服务器建立JDBC连接吗 - Can Java program establish JDBC Connection via Proxy Server 如何在不彻底更改程序的情况下用Java重置FileReader? - How to reset FileReader in java without having to change my program drastically? 每次运行java程序时如何使用logback来滚动日志文件 - How to use logback to roll log files everytime java program runs Java:无需身份验证即可建立SSL连接 - Java: establish SSL connection without authentication 我的程序是否可以永久保存变量而不在每次程序终止时重置它的值? - Can my program save a variable permanently without resetting it's value everytime the program terminates? 当键入“退出”而不使用system.exit()时,如何使用一段时间连续地要求用户输入并退出程序? - How can I use a while to continuously ask for input from a user and exit the program when “quit” is typed without using system.exit()? 如何在不运行Eclipse的情况下运行Java程序? - how can i run my java program without eclipse? 在Java中,如果我的程序没有-ea运行怎么办? - In Java, how can I fail if my program is run without -ea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM