我希望这是一个简单的问题,但我还没有找到答案,也没有找到好的资源。我在 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?
最佳答案
两种解决方案可能对您有所帮助。实际上,它有点复杂。解决方案1 通过c 代码使用mex 方法控制程序窗口。解决方案 2 也很复杂,只需使用 MATLAB 并行工具箱即可。嗯,我建议你使用解决方案 1。
解决方案一:
创建一个 cpp 文件,它控制您的交互式程序(即这里的窗口资源管理器)。代码如下。将代码复制并保存为“ctrlWindow.cpp”到您的 MATLAB 当前文件夹。
通过编译器lcc编译ctrlWindow.cpp:
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
在 MATLAB 命令中将 mex 文件作为 m 文件运行:
ctrlWindow('你的程序_窗口名称',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
//文件名: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
}
方案二:
matlabpool open 2
打开两个matlab后台,第一个控制你的第一个程序,第二个控制你的第二个程序。
关于Matlab:如何改变外部和内部窗口之间的焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13387351/