In the coding method of making an experiment we write a MATLAB script that builds the experiment in memory directly. This alternative can be useful for unusual experiments, or just if you prefer it. It isn’t actually that much harder—an experiment script isn’t really code in the usual sense, more like setting a list of parameters. And the steps correspond pretty much directly to what you would do in the visual method. A few commands specific to the coding method are all in <><PsychBench folder><>/coding.
The best way to learn the coding method is by examples. Below is a basic example taken from Overview earlier. For many more realistic examples see <><PsychBench folder><>/docs/demos—every demo has both visual and coding versions.
newExperiment
fileNames = [<cdsm>"red cone.png" "green cylinder.png" "blue cube.png"<cdsm>];
<cdkm>for<cdkm> fileName = fileNames
pic = pictureObject;
pic.fileName = fileName;
pic.height = 10;
pic.start.t = 0;
pic.end.response = true;
pic.report = <cdsm>"fileName"<cdsm>;
recorder = keyPressObject;
recorder.listenKeyNames = <cdsm>"space"<cdsm>;
recorder.start.t = 0;
recorder.report = <cdsm>"responseLatency"<cdsm>;
addTrial(picture, recorder);
<cdkm>end<cdkm>
nn = randomOrder(rep(1:3, 2));
setTrialList(nn);
Following are the general steps. You can see most of these steps in the example above.
Like in the visual method, the usual approach is to ignore trial repetition and order to run in when defining trials. Instead just define each distinct trial once, and in any order. In the coding method, distinct refers to property values of objects in trials. Since you can define trials in any order, you can use for loops through condition values to automate it, calling addTrial once per iteration. You can use nested for loops for combinations of conditions.