How to set intervals between pictures and maximum time?
Show older comments
I wrote a function to answer with keys ("d" or "u") to a series of pictures. how can I set a black fullscreen (2 sec) between each picture with a maximum presentation time (5 sec) for each pictures? If the subject doesn't press the key within 5 sec, the black screen should appear. I tried with tic toc but I had problems with either fullscreen or code. I am not a Matlab expert!
risultati=zeros(48,2);
figure;
hold on;
figure('units','normalized','outerposition',[0 0 1 1]);
hFig = gcf;
hAx = gca;
set(hAx,'Unit','normalized','Position',[0 0 1 1]);
set(hFig,'menubar','none')
set(hFig,'NumberTitle','off');
for a = 1:48
imshow(strcat(num2str(a), '.jpg'))
tStart = tic;
keyspressed = [];
k = 0;
while ~k
k = waitforbuttonpress;
tElapsed = toc(tStart);
currkey = get(gcf,'currentcharacter');
if strcmp(currkey,'d')
risp=0;
elseif strcmp(currkey,'u')
risp=1;
else
k=0;
end
risultati(a,1)=risp;
risultati(a,2)=tElapsed;
end
end
Answers (1)
Walter Roberson
on 6 Apr 2017
Edited: Walter Roberson
on 6 Apr 2017
0 votes
2 Comments
Andrea Zangrandi
on 6 Apr 2017
Edited: Andrea Zangrandi
on 6 Apr 2017
Walter Roberson
on 6 Apr 2017
Edited: Walter Roberson
on 6 Apr 2017
Install getkeywait . Then
entry_timeout = 5.0; %seconds
black_time = 2.0; %seconds
num_pic = 48;
pic_names = cellstr( num2str( (1:num_pic).', '%d.jpg') );
risultati = zeros(num_pic, 2);
hFig = figure('units','normalized','outerposition',[0 0 1 1]);
set(hFig, 'menubar', 'none')
set(hFig, 'NumberTitle', 'off');
hAx = axes('Parent', hFig, 'Unit', 'normalized', 'Position', [0 0 1 1], 'box', 'off', 'XColor', 'none', 'YColor', 'none', 'ZColor', 'none');
hold(hAx, 'on')
for a = 1 : num_pic
cla(hAx);
set(hAx, 'color', 'k');
pause(black_time);
h = imshow( pic_names{a} );
set(hAx, 'XColor', 'none', 'YColor', 'none', 'ZColor', 'none'); %compensate for imshow turning ticks back on
drawnow();
remaining_time = entry_timeout;
tStart = tic;
need_key = true;
while need_key
k = getkeywait(remaining_time);
assert(k ~= 0, 'Internal error waiting for keypress');
tElapsed = toc(tStart);
remaining_time = entry_timeout - tElapsed;
if k < 0
%user did not enter anything within timeout
break;
end
if isnan(k) %non-ASCII key, such as control
currkey = ' ';
else
currkey = char(k);
end
[tf, idx] = ismember(currkey, 'du');
if tf
risp = idx - 1;
risultati(a,1) = risp;
risultati(a,2) = tElapsed;
need_key = false;
end
end
end
cla(hAx);
I question your choice of imshow() for displaying the image; I think image() would be better for this purpose. imshow() reconfigures too much of the axes.
I went ahead and implemented the idea that the person has 5 seconds to get a choice of either 'u' or 'd', rather than 5 seconds between keys even if the key is not one of those two.
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!