Matlab:如何在外部窗口和内部窗口之间切换焦点?

4
我希望这是一个容易解决的问题,但我还没有找到答案,也没有找到一个好的资源。我正在用Matlab进行实验,在某些时候我们要调用外部程序。在一定时间后,我希望参与者返回Matlab进行调查,调查完成后继续他们的任务。问题是外部代码是交互式的,所以人们可能会在输入或点击操作时错过调查,调查完成后我不知道如何自动地返回他们打开的程序(尽管我知道他们何时完成调查并自动关闭浏览器)。我的示例代码如下:
    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?

根据您的描述,我假设您正在尝试将焦点重定向到外部程序(例如记事本)中的窗口?您不能创建一个用于文本输入的MATLAB图形窗口吗?如果您这样做,那么您将拥有该窗口的图形句柄,并且您可以简单地调用figure(fig_handle)来重定向焦点。 - jerad
是的,我们实际上正在试图将注意力转向游戏。其中一个变体有一个拼字游戏,但其他游戏更为复杂(例如Portal 2)。问题是许多这些游戏涉及大量按键,因此当我们的在线调查在Matlab中弹出时,人们会错过它,因为焦点返回到外部游戏。通过使用记事本解决问题应该可以很好地解决我的游戏问题。 - pstarsjonk
抱歉,我仍然不清楚您所关心的应用程序窗口是哪一个;您的游戏是否没有使用Matlab编码?如果您基本上试图使用Matlab作为控制几个其他外部程序的包装器,那么您可能不会找到简单的解决方案。无论如何,这很可能需要Java代码。此外,如果您更清楚地解释您正在尝试做什么以及为什么它不起作用,您在这里的运气会更好。 - jerad
好的。作为实验的一部分,我们使用眼动仪和其他各种生理设备,并在Matlab中进行协调。参与者玩游戏,然后在几分钟后填写有关这些游戏的调查问卷。这些游戏不是用Matlab编码的,通常是商业游戏。我们不需要跟踪他们对游戏的所有反应或类似的内容。我们想要做的是确保受试者玩游戏X分钟,然后填写调查问卷,然后继续玩游戏X分钟,然后再填写另一个调查问卷,以此类推。 - pstarsjonk
1个回答

3
两种解决方案可以帮助您。实际上,这有点复杂。解决方案1使用Mex方法通过C代码控制程序的窗口。解决方案2也很复杂,只需使用MATLAB并行工具箱。嗯,我建议您使用解决方案1。

解决方案1:

  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.

  2. 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
    
  3. run the mex file as m-file at MATLAB command:

    ctrlWindow('your_program_window_name',command);

即文件夹“myfold”的窗口名称为myfold,显示在窗口的左上角,输入以下命令:

ctrlWindow('myfold',6); 

这会最小化你的文件夹窗口。我建议你先最小化程序窗口,然后再将其最大化,这样参与者就会再次关注你的程序。
ctrlWindow('myfold',6);%minimize window
ctrlWindow('myfold',3);%maximize window and participants would focus on this window

命令在此处:

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
}

解决方案2:
matlabpool open 2

打开两个Matlab后台程序,使用第一个控制第一个程序,使用第二个控制第二个程序。

解决方案1可行!非常感谢。我一直在尝试查看PSSuspend和其他Windows进程,因为我不知道(因此没有想到)C ++。 - pstarsjonk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接