如何在 MATLAB 中并行化输入和显示? [英] How can I parallelize input and display in MATLAB?

查看:24
本文介绍了如何在 MATLAB 中并行化输入和显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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:

  1. 展示刺激
  2. 轮询键盘响应
  1. 如果没有检测到响应,循环回到 1
  2. 如果检测到响应,中断并继续执行脚本

这适用于键盘,因为第 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.

我目前的想法是尝试使用并行处理,这样一个线程显示刺激,另一个线程轮询键盘.我目前正在使用 Parallel Computing Toolbox 来执行此操作.我遇到的问题是我不知道如何将键盘输入定向到并行"线程.有谁知道(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

我能够通过一些简单的绘图例程成功运行上述函数,并用简单的 pause 语句和返回值.我不确定 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 函数的文档:<代码>createJobcreateTask提交waitForStatedestroy.

Here's the documentation for the Parallel Computing Toolbox functions used in the above code: createJob, createTask, submit, waitForState, destroy.

这篇关于如何在 MATLAB 中并行化输入和显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆