简体   繁体   English

如果Matlab以GUI开始,如何从MEX文件中检查

[英]How to check from MEX-file if Matlab started with GUI

I already tried to find an answer to this problem myself, and asked my question at Matlab Central without getting any response. 我自己已经尝试找到这个问题的答案,并在Matlab Central问我的问题而没有得到任何回复。 Now I'm hoping probably one of you can help me solve my problem. 现在我希望你们中的一个可以帮助我解决我的问题。 Here's my (improved) question: 这是我的(改进的)问题:

The headline already describes pretty well what I have to do: I have to check from a MEX-file whether Matlab R2013a on Linux has been started with or without GUI. 标题已经很好地描述了我必须要做的事情:我必须从MEX文件中检查Linux上的Matlab R2013a是否已经启动了GUI。

Background: I run a C/C++-program (from wich I can use the sources, but am not allowed to change them, only to add new files if neccessary!) from Matlab. 背景:我运行一个C / C ++ - 程序(我可以使用这些源代码,但不允许更改它们,只有在必要时才添加新文件!)来自Matlab。 I wrote several (additional) MEX-files that allow the program to use mexCallMATLAB to evaluate m-files. 我写了几个(附加的)MEX文件,允许程序使用mexCallMATLAB来评估m文件。 Now I need to know where I have to direct the output of the C/C++-program, depending on if Matlab has been started with or without GUI. 现在我需要知道在哪里我必须指导C / C ++程序的输出,这取决于Matlab是否已经启动了GUI。 I need to get this information from a function callable from my MEX-files. 我需要从我的MEX文件中调用的函数中获取此信息。 Up to now I can only redirect the output of the C/C++-program by changing hard-coded parameters, but I already can direct it to the right outputs, meaning either console without, or the Matlab command window with GUI. 到目前为止,我只能通过更改硬编码参数来重定向C / C ++程序的输出,但我已经可以将它指向正确的输出,这意味着没有控制台,或者带有GUI的Matlab命令窗口。

Up to R2012x, a check was possible using the C++-function isatty() , but from R2013a on, this check does not work anymore, meaning Matlab always appears to be started from console only, even though if it has been started with GUI. 直到R2012x,可以使用C ++ - 函数isatty() ,但是从R2013a开始,此检查不再起作用,这意味着Matlab似乎始终只从控制台启动,即使它已经启动了GUI。

Does anyone of you know a function like this, or another solution for my problem? 你们中的任何人都知道这样的功能,还是我的问题的另一种解决方案?

Thank you people in advance! 提前谢谢大家!

Greetings, mindm49907 问候,mindm49907

Call usejava('desktop') via mexCallMATLAB . 通过mexCallMATLAB调用usejava('desktop') From the docs for usejava : 来自usejava的文档

Syntax 句法

tf = usejava(feature)

... ...

Java feature, specified as one of these values: Java功能,指定为以下值之一:

'awt' Java GUI components in the Abstract Window Toolkit (AWT) components are available. 'awt'抽象窗口工具包(AWT)组件中'awt' Java GUI组件可用。

'desktop' MATLAB interactive desktop is running. 'desktop' MATLAB交互式桌面正在运行。

'jvm' Java Virtual Machine software (JVM) is running. 'jvm'虚拟机软件(JVM)正在运行。

'swing' Swing components (Java lightweight GUI components in the Java Foundation Classes) are available. 'swing' swing'Swing组件(Java基础类中的Java轻量级GUI组件)可用。

atDesktop.cpp atDesktop.cpp

#include "mex.h"

bool atMLDesktop()
{
    mxArray *tf(0);
    mxArray *permuteRHSArgs = mxCreateString("desktop");
    mexCallMATLAB(1, &tf, 1, &permuteRHSArgs, "usejava");

    return mxIsLogicalScalarTrue(tf);
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    plhs[0] = mxCreateLogicalScalar(atMLDesktop());
}

Test 测试

From MATLAB desktop: 从MATLAB桌面:

>> atDesktop
ans =
     1
>> tf = atDesktop
tf =
     1

From terminal or bare command window: 从终端或裸命令窗口:

» tf = atDesktop
tf =
     0

You can also check com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame in MATLAB the same way as above, but usejava is supported by MathWorks. 您也可以使用与上面相同的方式在MATLAB中检查com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame ,但MathWorks支持usejava

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM