Flashing rectangles on screen at different frequencies

11 views (last 30 days)
Hello All
I'm trying to implement a simple interface to perform EEG SSVEP Experiments, this interface is simply a figure which has two small rectangles, located at the center-left and center-right corners of the screen, I specify two input frequencies and rectangles flash in those frequencies.
I have tried two different approaches, both have failed so far... I'll quickly describe them and kindly request help from you guys about why are they failing or what other different approach can I use in order to perform this correctly, I thank you all in advance.
Method 1: Using MATLAB timer objects, they are initialized as follows
timer_rec1 = timer;
set(timer_rec1, 'executionMode', 'fixedRate');
set(timer_rec1, 'TimerFcn', 'flashRectangle(''left'');');
set(timer_rec1, 'Period', 1/ifreq);
start(timer_rec1);
...
stop(timer_rec1);
delete(timer_rec1);
clear timer_rec1;
Rectangles are draw/Update either this way
rectangle('Parent', gca, 'Position', rec_pos, 'FaceColor', ...
rec_fcolor, 'EdgeColor', rec_ecolor);
Or this way
set(rec, 'FaceColor', rec_fcolor, 'EdgeColor', rec_ecolor);
This is supposed to call flashRectangle at approx. ifreq Hz, the function just draw the rectangle either on the left or right position and changes its color either to red o background (creating the flashing effect). The problem here is that for some strange reason, after some time (like 30 seconds) the drawing speed of the rectangles start to decrease, really weird, if the input frequency is around 13 Hz that rectangle flashes slower and slower after a few seconds. I'have tried using handles of the rectangle objects (which parent is the figure's axis) and also redrawing the rectangle on every call, I have use drawnow, refresh, small pauses, the effect is always the same.
Method 2: using cycles within a for loop. This approach is much simpler I just set rectangles to update every X and Y iterations of a for loop, this gives "consistent" drawing speed BUT every Z (unpredictable) seconds there is a small lag while plotting, you can clearly see it, this lag happens independently on every rectangle, after the lag they "catch up" their original speed but the thing is the flashing frequency is not constantly maintained, Also I don't know how to determine accurately the frequency those rectangles are flashing.
I have revised the code, trying to set up the figure, axis and rectangles in a fast way but still don't work. Again I appreciate your help, thanks.

Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!