# Trial event figure maker

This function makes a picture like this:

I wrote this because I was too lazy to do it by hand :) You set it up using a task_specs structure illustrated as the default in the m-file. The stimulus displays would probably require most extra work of course.

As always, remember to save the figure in a suitable format (e.g. tiff) if you plan on ever having it printed!

```function task_figure(varargin)

%
%
%

if length(varargin) >= 1,
else,
end;
task_specs.times = [0 1500 3500 4250 5500];

end;

if length(varargin) >= 2,
angle = varargin{2};
else
angle = pi / 8;
end;

t_buffer = 0; %0.1 * (t_end - t0);
t0 = t0 - t_buffer;
t_end = t_end + t_buffer;
timeLineX = [t0 t_end * cos(angle)];
timeLineY = [t0 t_end * sin(angle)];
timeLineL = sqrt(diff(timeLineX) .^ 2 + diff(timeLineY) .^ 2);

figure(1); clf; hold on;
minx = timeLineX(1) - 0.1 * abs(diff(timeLineX));
maxx = timeLineX(2) + 0.1 * abs(diff(timeLineX));
miny = timeLineY(1) - 1.2 * task_specs.plotting.screenSize * timeLineL;
maxy = timeLineY(2) + 1.6 * task_specs.plotting.eventArrowLength * timeLineL;
XLim(sort([minx maxx]));
YLim(sort([miny maxy]));
set(gca, 'XTick', []);
set(gca, 'YTick', []);
fprintf('Now stretch to beautifulness\n');
dx = diff(xlim);
dy = diff(ylim);
pos = get(gcf, 'Position');
if dx > dy,
else,
end;
set(gcf, 'Position', pos);

time_line = plot(timeLineX, timeLineY, 'k-'); set(time_line, 'LineWidth', 2);
arrow(timeLineX, timeLineY, 0.1, pi/8, [0 0 0], '-', 2);

% Events
screenX = timeLineX(1) + diff(timeLineX) * (task_specs.times(iScreen) + t_buffer) / (t_end + t_buffer);
screenY = timeLineY(1) + diff(timeLineY) * (task_specs.times(iScreen) + t_buffer)/ (t_end + t_buffer);
x = [screenX screenX]
y = [screenY screenY + task_specs.plotting.eventArrowLength * timeLineL]
arrow(x, y, 0.2, pi/8, [0 0 0], '-', 1);
% once for extent...
ext = get(t, 'Extent');
xshift = ext(3) / 2;
yshift = ext(4) / 2;
set(t, 'Visible', 'off');
t = text(x(2) - xshift, y(2) + yshift, task_specs.names{iScreen});
end;
end;

% interval names
screenX1 = timeLineX(1) + diff(timeLineX) * (task_specs.times(iScreen) + t_buffer) / (t_end + t_buffer);
screenY1 = timeLineY(1) + diff(timeLineY) * (task_specs.times(iScreen) + t_buffer)/ (t_end + t_buffer);
screenX2 = timeLineX(1) + diff(timeLineX) * (task_specs.times(iScreen + 1) + t_buffer) / (t_end + t_buffer);
screenY2 = timeLineY(1) + diff(timeLineY) * (task_specs.times(iScreen + 1) + t_buffer)/ (t_end + t_buffer);
screenX = mean([screenX1 screenX2]);
screenY = mean([screenY1 screenY2]);
intArrowLength = 1.5 * screenSize0;
x = [screenX screenX + cos(pi / 4) * intArrowLength];
y = [screenY screenY - sin(pi / 4) * intArrowLength];
arrow(x, y, 0.2, pi/8, [0 0 0], '-', 1);
% once for extent...
ext = get(t, 'Extent');
set(t, 'Visible', 'off');
t = text(x(2) + xshift, y(2) - yshift, task_specs.interval_names{iScreen});
end;
end;

% screens
screenX = timeLineX(1) + diff(timeLineX) * (task_specs.times(iScreen) + t_buffer) / (t_end + t_buffer);
screenY = timeLineY(1) + diff(timeLineY) * (task_specs.times(iScreen) + t_buffer)/ (t_end + t_buffer);
pos = [screenX - screenSize0 / 2 screenY - screenSize0 screenSize0 screenSize0];
verticesX = [pos(1); pos(1) + pos(3); pos(1) + pos(3); pos(1)];
verticesY = [pos(2); pos(2); pos(2) + pos(4); pos(2) + pos(4)];
fill(verticesX, verticesY, [1 1 1]);
rect0 = rectangle('Position', pos);
for iStim = 1:nStim,
x0 = pos(1) + pos(3) / 2;
dStim = 0.8 * pos(4) / nStim;
y0 = (pos(2) + 0.9 * pos(4)) - dStim * iStim;
% once for extent...
ext = get(t, 'Extent');
xshift = ext(3) / 2;
yshift = ext(4) / 2;
set(t, 'Visible', 'off');
t = text(x0 - xshift, y0 + yshift, task_specs.stim{iScreen}{iStim});
end;
end;
end;

function arrow(varargin)

%
% x_comp and y_comp must contain two values.
% Trailing plotting parameters may be ommitted. Any plotting parameter can be specified as [] for defaults.

axes0 = gca;
if length(varargin{1}) == 1,
x0 = varargin{1};
y0 = varargin{2};
r = varargin{3};
angle = varargin{4};
x_comp = [x0 x0 + r * cos(angle)];
y_comp = [x0 x0 + r * sin(angle)];
plotParIndex = 5;
else,
x_comp = varargin{1};
y_comp = varargin{2};
r = sqrt(diff(x_comp) .^ 2 + diff(y_comp) .^ 2);
angle = atan2(diff(y_comp), diff(x_comp));
plotParIndex = 3;
end;

if length(varargin) >= plotParIndex,
end;
else,
end;
if length(varargin) >= plotParIndex + 1,
end;
else,
end;
if length(varargin) >= plotParIndex + 2,
color = varargin{plotParIndex + 2};
if isempty(color),
color = [0 0 0];
end;
else,
color = [0 0 0];
end;
if length(varargin) >= plotParIndex + 3,
linestyle = varargin{plotParIndex + 3};
if isempty(linestyle),
linestyle = '-';
end;
else,
linestyle = '-';
end;
if length(varargin) >= plotParIndex + 4,
linewidth = varargin{plotParIndex + 4};
if isempty(linewidth),
linewidth = 0.1;
end;
else,
linewidth = 0.1;
end;

holding = get(axes0, 'NextPlot');
if strcmp(holding, 'replace') == 1,
cla;
end;

% The main line
drawLine(x_comp, y_comp, color, linestyle, linewidth);

% The head: draw on origin on a lying arrow, then rotate and translate.

% whitespace if necessary
xlim0 = get(gca, 'XLim');
ylim0 = get(gca, 'YLim');
xlim2 = [min([xlim0(1); xlim1(1)]) max([xlim0(2); xlim1(2)])];
ylim2 = [min([ylim0(1); ylim1(1)]) max([ylim0(2); ylim1(2)])];
set(gca, 'XLim', xlim2);
set(gca, 'YLim', ylim2);

%set(axes0, 'NextPlot', holding);

function line0 = drawLine(x_comp, y_comp, color, linestyle, linewidth);
line0 = line(x_comp, y_comp);
set(line0, 'Color', color);
set(line0, 'LineStyle', linestyle);
set(line0, 'LineWidth', linewidth);