Hellopsychophysicist - Matlab version

This section explains line by line the Matlab version of the HelloPsychophysicist project (source: HelloPsychophysicist.m). This example demonstrates how to develop Matlab projects using psychWithJava package in general.

There are four common steps for Matlab projects with psycWithJava package

  1. Import psychWithJava and other packages

  2. Normally you would have to write the entire path to a class in your code, alternatively you can import the classes you need and eliminate the long names. Place the following line near the top of your program
    import psychWithJava.*;
    Alternatively you could import only a single class from a package
    import psychWithJava.FullScreen;
    Note that psychWithJava must be in your javaclasspath. See instructions on setting up your system for Matlab.
  3. Create a Java object

  4. fs = FullScreen();
    place this inside HelloPsychophysicist.m file, don't try to execute it yet, your system may hang (press ESC if it does). Once a java object is created, one can use methods (functions) of that java object just as one would do in a normal pure java program. FullScreen, the most important class in the psychWithJava package, provides tools for psychophysics programming. See FullScreen() for more information on constructing a FullScreen object.

  5. Initialize certain aspects of your object

  6. There are a few adjustments you can make for your FullScreen object. For example, here let's set the number of video buffers to 2, which by default is 1
    See also setNBuffers(int).

  7. Provide the experiment/animation code inside a try-catch clause

  8. Matlab has an error handling mechanism, similar to Java. It only lacks the finally{} part, causing some minor inconvenience.
      fs = FullScreen();
      % Your animation here
      % ....
    Always put closeScreen() both at the end of try and inside the catch part. This way if your program crashes it is less likely to hang your entire system. rethrow(lasterror) prints out a report from the exception.

    Above 4 steps will be nearly identical in all examples, except the names of files and corresponding public classes.

The experiment/animation using the methods of FullScreen

    This is the part that will vary depending on your experiment. I explain the implementation for the HelloPsychophysicist project below.

  • Display Text, wait for a while
  • Display some text, draw it actually on the screen and wait for 2 seconds without doing anything
        'Hello Psychophysicist (from within Matlab)');
    displayText(String) method displays the text stored in a String object at the center of the video buffer. It is overloaded, you can define the position where you want to place the upper left corner of the text on your screen by invoking displayText(int, int, String). Next you have to invoke updateScreen() method. Because although you invoked displayText() method, you only painted the text on a back video buffer. To actually display on the computer screen this back buffer should be brought to front by updateScreen(). pause() is a Matlab function and pauses the execution of the program for given amount of time (in seconds). Alternatively you could use the Thread.sleep() method of core Java platform to pause.

  • Blank the screen, hide the cursor
  • fs.blankScreen();
    If you don't blank the screen (back buffer), you will paint over the existing screen. This is useful for animations when you want to update only a portion of the screen. But in this example we need to clean it first. (try and see what happens if you don't do that.) See blankScreen() and hideCursor().

  • Read in and display an image
  • bi1 = im2java2d(imread('psychophysik.png','PNG'));
    There are many ways of reading in an image. One option is using Matlab's built-in image reading function imread(). Matlab also offers a function called im2java2d(), which converts the matlab image into a Java image (BufferedImage). Alternatively you can use the Java methods to read in images as follows
    bi1 ='psychophysik.png'));
    In case you are reading the image from a file I recommend using the pure Java method. If you are numerically generating your stimulus, for example a Gabor patch, then using im2java2d can be the better option. Once you created a BufferedImage you can display it on the screen using displayImage() method
    displayImage() method is overloaded, you can invoke it with additional parameters in order to place your image at a location other then the center of the screen, see displayImage(int, int, BufferedImage).

    See the entire lising of HelloPsychophysicist.m.