Matlab: How to change focus between external and internal windows?

focus, matlab

Solution

Two solutions may help you. Actually, it is a little complicate. Solution 1 use mex method control window of program by c code. Solution 2 is also complicate, just use MATLAB parallel toolbox. Hmm, I suggest your use solution 1.

solution 1:

Create a cpp file, which controls your interactive program(i.e. window explorer here). code is below. copy and save the code as "ctrlWindow.cpp" at your MATLAB current folder.

compile ctrlWindow.cpp by compiler lcc:

mex -setup % choose compiler: type this command at MATLAB command, then choose lcc complier on windows 32 system

mex ctrlWindow.cpp % compile cpp: you would find ctrlWindow.mexw32 at current folder

run the mex file as m-file at MATLAB command:

ctrlWindow('your_program_window_name',command);

i.e. window name of a folder "myfold" is myfold which is display on the top left of window, type command:

ctrlWindow('myfold',6); 

this would minimize your folder window. I suggest you minimize your program window firstly, then maximize it, and participants would focus on your program again:

ctrlWindow('myfold',6);%minimize window
ctrlWindow('myfold',3);%maximize window and participants would focus on this window

command is here:

HIDE             0
SHOWNORMAL       1
NORMAL           1
SHOWMINIMIZED    2
SHOWMAXIMIZED    3
MAXIMIZE         3
SHOWNOACTIVATE   4
SHOW             5
MINIMIZE         6
SHOWMINNOACTIVE  7
SHOWNA           8
RESTORE          9
SHOWDEFAULT      10
FORCEMINIMIZE    11
MAX              11

//filename:ctrlWindow.cpp

#include <windows.h>

#include "mex.h"


void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
    mxChar* winName; //name of window wanted to be found
    HWND hwnd; //handle of window
    int command; //command of control window
    // check number of input
    if(nrhs!=2)
        mexErrMsgTxt("input must be 2");
    // check class of input
    if (mxIsChar(prhs[0]))
        winName=mxGetChars(prhs[0]);//get name of window
    else
        mexErrMsgTxt("input 1 should be char -- name of window");
    if (mxIsDouble(prhs[1]))
    {
        command = (int) mxGetScalar(prhs[1]);//get command
        if(command<0 || command >11)//check command
            mexErrMsgTxt("No such command!!!");
    }
    else
        mexErrMsgTxt("input 2 should be a double");
    // find window
    hwnd = FindWindowW(NULL, (LPCWSTR)winName);
    if(NULL==hwnd)
    {
        MessageBoxW(NULL,(LPCWSTR) L"Can't find the window!!!",NULL,MB_OK);
        return;
    }
    ShowWindow(hwnd, command);//control the window
}

Solution 2:

matlabpool open 2

open two matlab background, use first control your first program, use second one control your second program.

Problem

I'm hoping this is an easy problem, but I haven't figured out the answer yet nor seen a good resource for this. I'm running an experiment in Matlab, and at times we call to an external program. After a certain amount of time, I want the participants to return to Matlab to take a survey, and then resume their task after they are done. The problem is the external code is interactive, so a person could be typing or clicking and not see the survey open up, and after completing the survey I don't know how to automatically return them to their open program (although I know when they are done with their survey and automatically close the browser). My toy code example would be: ``` system('start \max notepad.exe') pause(60) %After x seconds a web page opens up in Matlab, how to ensure users see it? web('cnn.com') %I have code that will close this after they click on a certain link %After close browser, how to return to notepad where they left off? ```

Original source