问题描述
我在MATLAB中使用 Psychtoolbox 来运行行为心理学范例.作为范例的一部分,用户必须查看视觉刺激并使用某种输入机制对其做出响应.对于键盘,其工作方式如下:
I'm using Psychtoolbox in MATLAB to run a behavioral psychology paradigm. As part of the paradigm, users have to view a visual stimulus and respond to it using some input mechanism. For a keyboard, this works as follows:
- 显示刺激
- 投票键盘以进行响应
- show stimulus
- poll keyboard for response
- 如果未检测到响应,请循环回到1
- 如果检测到响应,请中断并继续执行脚本
这对于键盘工作正常,因为步骤2花费1-2毫秒之间的时间.当我使用备用输入机制时,问题就来了.在这种情况下,第2步大约需要20毫秒. (我需要这种替代输入来进行研究,这应该被认为是不变的事实.)由于刺激在很短的时间内发生变化,因此增加的延迟使任务中断.
This works fine for a keyboard, as step 2 takes between 1-2 ms. The problem comes when I use an alternate input mechanism; in that case, step 2 takes ~20 ms. (I need this alternate input to run the study, and that should be considered immutable fact.) As the stimulus changes with a very short timespan, this added delay breaks the task.
我目前的想法是尝试使用并行处理,以便一个线程显示刺激,而另一个线程轮询键盘.我目前正在使用并行计算工具箱来执行此操作.我遇到的问题是我不知道如何将键盘输入定向到并行"线程. 有人知道(1)是否可以将键盘输入定向到线程/是否有线程将可视信号发送到监视器,如果是,(2)怎么做?
My current thought is to try to use the parallel processing, such that one thread shows the stimulus, and another thread polls the keyboard. I'm currently using the Parallel Computing Toolbox to do this. The problem I'm having is that I don't know how to direct keyboard input to a "parallelized" thread. Does anyone know (1) whether it's possible to direct keyboard input to a thread / have a thread send a visual signal to a monitor, and if yes, (2) how to do it?
此外,如果有人对如何解决此问题有更好的主意,我将不知所措.
Also, if anyone has any better ideas as to how to approach this problem, I'm all ears.
推荐答案
根据此MATLAB新闻组线程,似乎线程无法修改图形对象.只有台式机MATLAB客户端才能做到这一点.这意味着您无法处理来自线程的图形更新,我可以在尝试时确认这一点,并且无法修改图形,甚至无法修改根对象.
According to this MATLAB newsgroup thread, it appears that threads can't modify graphics objects. Only the desktop MATLAB client can do that. This means that you can't handle updating of graphics from a thread, and I can confirm this as I tried it and wasn't able to modify figures or even the root object from a thread.
但是,我认为当线程处理输入的轮询时,您也许可以在MATLAB中进行主要图形的更新.这是一个示例函数,用于连续更新显示,直到等待 KbCheck
输入的线程运行完毕:
However, I think you may be able to do the main graphics updating in MATLAB while a thread handles polling for your input. Here's a sample function for continuously updating a display until a thread waiting for input from KbCheck
is finished running:
function varargout = plot_until_input
obj = createJob(); %# Create a job
task = createTask(obj,@get_input,4,{deviceNumber}); %# Create a task
submit(obj); %# Submit the job
waitForState(task,'running'); %# Wait for the task to start running
%# Initialize your stimulus display here
while ~strcmp(get(task,'State'),'finished') %# Loop while the task is running
%# Update your stimulus display here
end
varargout = get(task,'OutputArguments'); %# Get the outputs from the task
destroy(obj); %# Remove the job from memory
%#---Nested functions below---
function [keyIsDown,secs,keyCode,deltaSecs] = get_input(deviceNumber)
keyIsDown = false;
while ~keyIsDown %# Keep looping until a key is pressed
[keyIsDown,secs,keyCode,deltaSecs] = KbCheck(deviceNumber);
end
end
end
我能够使用一些简单的绘图例程成功运行上述功能,并使用简单的暂停语句和一个返回值.我不确定KbCheck
是否可以在线程中工作,但是希望您能够适应您的需求.
I was able to successfully run the above function with some simple plotting routines and replacing the code in get_input
with a simple pause statement and a return value. I'm unsure whether KbCheck
will work in a thread, but hopefully you will be able to adapt this for your needs.
以下是上述代码中使用的Parallel Computing Toolbox函数的文档: createJob
, createTask
, submit
, waitForState
, destroy
.
Here's the documentation for the Parallel Computing Toolbox functions used in the above code: createJob
, createTask
, submit
, waitForState
, destroy
.
这篇关于如何在MATLAB中并行化输入和显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!