简体   繁体   中英

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. 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.

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. I wrote several (additional) MEX-files that allow the program to use mexCallMATLAB to evaluate m-files. 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. I need to get this information from a function callable from my MEX-files. 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.

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.

Does anyone of you know a function like this, or another solution for my problem?

Thank you people in advance!

Greetings, mindm49907

Call usejava('desktop') via mexCallMATLAB . From the docs for usejava :

Syntax

tf = usejava(feature)

...

Java feature, specified as one of these values:

'awt' Java GUI components in the Abstract Window Toolkit (AWT) components are available.

'desktop' MATLAB interactive desktop is running.

'jvm' Java Virtual Machine software (JVM) is running.

'swing' Swing components (Java lightweight GUI components in the Java Foundation Classes) are available.

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:

>> 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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