I am just trying to program a reaction test with a MATLAB GUI.
I've got one button which, after every click, appears at a random place and afterwards the needed time for the next click is shown.
Now I want that this button dissapears after clicking on it 10 times.
How can I program, that after the 10th click it just executes "close all;"?
Here is some code which does what you want.
As #Li-aung mentioned, I added a counter which keeps track of the number of times the pushbutton was pushed. The counter is stored in the handles structure of the figure; however you could make it a global variable for simplicity.
Here is the code with comments:
function RandomButton(~)
hFig = figure('Position',[100 100 200 200],'Visible','off');
handles.CounterText = uicontrol('Style','text','Position',[50 150 60 30],'String','Counter')
handles.DisplayCounterText = uicontrol('Style','text','Position',[50 100 60 30],'String','0')
handles.Button = uicontrol('Style','pushbutton','position',[50 50 60 30],'String','Push here','Callback',#(s,e) Push);
handles.PushCounter = 0; %// Initialize counter
movegui(gcf,'center')
set(hFig,'Visible','on')
guidata(hFig,handles)
function Push
handles = guidata(hFig);
handles.PushCounter = handles.PushCounter +1;
set(handles.DisplayCounterText,'String',num2str(handles.PushCounter));
if handles.PushCounter < 10 %// Assign condition to stop.
set(hFig,'Visible','off');
set(hFig,'Position',[1000*rand(1) 1000*rand(1) 200 200]); %// Assign random position
set(hFig,'Visible','on');
guidata(hFig,handles) %// Update handles structure. Important!
else
close all
end
end
end
Here is a screenshot of the GUI:
Hope that helps get you started!
Related
I am running a long data preload within a timer callback, and I would like to be able to stop the callback halfway through with an outside input (for instance, the click of a GUI button).
The stop() function will stop the timer calls, but not the callback function itself.
Here is a simple example:
timerh = timer('TimerFcn' , #TimerCallback,'StartDelay' , 1, 'ExecutionMode' , 'singleShot');
NeedStopping = false;
start(timerh)
disp('Running')
pause(1)
disp('Trying to stop')
NeedStopping = true;
function TimerCallback(obj, event)
% Background data loading in here code in here; in this example,
% the callback simply displays numbers from 1 to 100
for k = 1 : 100
drawnow(); % Should allow Matlab to do other stuff
NeedStopping = evalin('base' , 'NeedStopping');
if NeedStopping
disp('Should stop now')
return
end
disp(k)
pause(0.1)
end
end
I expect this script to display numbers between 1 and (approximately) ten, but the timer callback does not stop until 100.
Strangely, the code reaches the line just before before the pause(1) and correctly prints 'Running', but then it stops there and waits for the timer to finish.
Even more perplexing, if I change the 1 second pause to 0.9 seconds, the timer stops immediately with this output:
Running
Trying to stop
Should stop now
I am aware that Matlab is mostly single-threaded, but I thought the drawnow() function should allow it to process other stuff.
Edit: Specific use behind my question:
I have a GUI with a "next" button that loads several images and shows them all side by side. The images are large so loading takes time; therefore, while the user looks at the pictures, I want to preload the next set. This can be done in the background with a timer, and it works.
However, if the user clicks "next" before the preloading has finished, I need to stop it, show the current images, and launch the preloading for the next step. Hence, the timer needs to stop during callback execution.
This is a demo of how to set up an interruptible callback. The way your example is set up I didn't see the need for an actual timer so I made it as a standard button callback.
note: If you are dead set on using it for a timer you can use exactly the same solution, just assign the startProcess callback to the timer instead of the gui button.
function h = interuptible_callback_demo
% generate basic gui with 2 buttons
h = create_gui ;
guidata( h.fig , h )
% create application data which will be used to interrupt the process
setappdata( h.fig , 'keepRunning' , true )
end
function startProcess(hobj,~)
h = guidata( hobj ) ;
% set the 'keepRunning' flag
setappdata( h.fig , 'keepRunning' , true )
% toggle the button states
h.btnStart.Enable = 'off' ;
h.btnStop.Enable = 'on' ;
nGrainOfSand = 1e6 ;
for k=1:nGrainOfSand
% first check if we have to keep running
keepRunning = getappdata( h.fig , 'keepRunning' ) ;
if keepRunning
% This is where you do your lenghty stuff
% we'll count grains of sand for this demo ...
h.lbl.String = sprintf('Counting grains of sands: %d/%d',k,nGrainOfSand) ;
pause(0.1) ;
else
% tidy up then bail out (=stop the callback)
h.lbl.String = sprintf('Counting interrupted at: %d/%d',k,nGrainOfSand) ;
% toggle the button states
h.btnStart.Enable = 'on' ;
h.btnStop.Enable = 'off' ;
return
end
end
end
function stopProcess(hobj,~)
h = guidata( hobj ) ;
% modify the 'keepRunning' flag
setappdata( h.fig , 'keepRunning' , false )
end
function h = create_gui
h.fig = figure('units','pixel','Position',[200 200 350 200]) ;
h.btnStart = uicontrol('style','pushbutton','string','Start process',...
'units','pixel','Position',[50 100 100 50],...
'Callback',#startProcess) ;
h.btnStop = uicontrol('style','pushbutton','string','Stop process',...
'units','pixel','Position',[200 100 100 50],...
'Callback',#stopProcess,'enable','off') ;
h.lbl = uicontrol('style','text','string','','units','pixel','Position',[50 20 200 50]) ;
end
To see it in action:
I am delivering a psychology experiment in Matlab, where screens with questions will be presented to subject. The screen will also collect and display the subjects' responses. For example: Screen displays '2+3' and also displays what participant types (say, 99999), until they press enter.
GOAL: get it to stop displaying the question after 16 seconds if the participant has not yet pressed enter. (That is, stop displaying the screen if time=16sec OR if subject presses Enter.)
The problem revolves around the following code:
While CurrentTime<TimeOut
respond=GetChar() <-(Waits till user press enter)
end
So whatever the statements we add before/after capturing respond statements is not executed.
Any help on how to work around this issue would be greatly appreciated! Thanks.
Here is an example, I presented an oval as an example, you can obviously replace that with whatever your stimuli are. Enter and Return are separate keys, I wasn't sure which you're looking for, so in the example the loop looks for either.
%% include at top of experiment / block
waitForResponseSeconds = 16; % number of second to wait for a response
enterKey = KbName('enter'); % numeric code for enter key
returnKeys = KbName('return'); % numeric code for return key(s)
responseKeys = [enterKey returnKeys];
wPtr = Screen('OpenWindow', 0, 0, [0 0 400 400]);
%% within the trial loop:
hasResponded = 0;
% present the stimulus (here the window pointer is called wPtr, you may
% need to adjust this depending on what you named the window pointer.
Screen('FillOval', wPtr, [100 0 100], [0 0 400 400]);
[~, Onset] = Screen('Flip', wPtr);
while ~hasResponded && ((GetSecs - Onset) <= waitForResponseSeconds)
[keyIsDown, secs, keyCode] = KbCheck;
if any(keyCode(responseKeys))
rt = 1000.*(secs-Onset); % get response time
hasResponded = 1;
end
% Wait 1 ms before checking again to prevent
% overload of the machine at elevated priority
WaitSecs(0.001);
end
%% end of exp
sca;
EDIT: See end of post for code to automatically reproduce the issue without the mouse/keyboard input and for pictures describing the issue.
Please let me know if any of the following explanation or code requires further clarification.
I'm allowing a user to click on an axis and scatter points appear where they click. Left clicking results in larger scatter markers, and right clicking results in smaller scatter markers. Pressing enter adds a new set (represented by scatter markers switching between yellow and red). Pressing backspace should delete the most recent marker, and consecutively delete markers back through the set and into previous sets if continued to be pressed.
This works fine if I use only one size marker, however, if I allow the size to be determined by the left/right click, then upon clicking backspace to delete previous points, it will correctly delete the points until you arrive at the first point (the earliest point, not the first you try to delete) which had a size change. The still appear on the figure as if they were a separate scatter plot, even though they are one scatter plot, and the data the plot is set from has been deleted (and the scatter data set to that deleted data set).
Here is my example code:
function CodeToReplicateIssue()
a = get(0,'ScreenSize');
set(0,'DefaultFigurePosition',[a(3)*.3,a(4)*.1,a(3)*.4,a(4)*.8]);
mainFig = figure;
set(mainFig, 'WindowButtonDownFcn', #mouseDown);
set(mainFig,'WindowKeyPressFcn', #keyPress);
pfTitle = 'Next Set: Enter Delete Previous Point: Backspace';
colormap('gray');
imagesc(zeros(512,512));
set(gca,'XTick',[]);
set(gca,'YTick',[]);
axis image;
title(pfTitle)
hold on;
cStrs2 = {'r','y'};
pfTracks = {[]};
pfTracksRef = {[]};
pfTrackI = 1;
pfScatters{1} = scatter([],[],5,cStrs2{2});
function mouseDown(source,callbackData)
%display('mouseDown');
pos = get (gca, 'CurrentPoint');
pos = pos(1,1:2);
pfTracksRef{pfTrackI}(end+1) = ~strcmp(get(source,'SelectionType'),'alt');
pfTracks{pfTrackI}(end+1,1:2) = pos;
if(numel(pfScatters)<pfTrackI)
pfScatters{pfTrackI} = scatter(pos(1),pos(2),getSizes(pfTrackI),cStrs2{mod(pfTrackI,2)+1});
else
set(pfScatters{pfTrackI},{'XData','YData','SizeData'},{pfTracks{pfTrackI}(:,1),pfTracks{pfTrackI}(:,2),getSizes(pfTrackI)});
end
end
function keyPress(source,eventdata)
%display(eventdata.Key);
if(strcmp(eventdata.Key,'return'))
if(isempty(pfTracks{pfTrackI}))
%do nothing
else
refreshScatter('pfScatters');
pfTrackI = pfTrackI+1;
pfTracks{pfTrackI} = [];
pfTracksRef{pfTrackI} = [];
end
elseif(strcmp(eventdata.Key,'backspace'))
if(isempty(pfTracks{pfTrackI}) && numel(pfTracks)>1)
pfTracks(pfTrackI) = [];
pfTracksRef(pfTrackI) = [];
if(numel(pfScatters) >= pfTrackI)
pfScatters(pfTrackI:end) = [];
end
pfTrackI = pfTrackI -1;
end
if(numel(pfTracks{pfTrackI}))
pfTracks{pfTrackI}(end,:) = [];
pfTracksRef{pfTrackI}(end) = [];
end
refreshScatter('pfScatters');
end
end
function refreshScatter(strOption)
if(nargin<1)
strOption = '';
end
if(any(strcmp(strOption,{'','pfScatters'})))
for n = 1:numel(pfScatters)
if(numel(pfTracks{n})>0)
set(pfScatters{n},{'XData','YData','SizeData','MarkerEdgeColor'},{pfTracks{n}(:,1),pfTracks{n}(:,2),getSizes(n),cStrs2{mod(n,2)+1}});
else
set(pfScatters{n},{'XData','YData'},{[],[]});
end
end
end
end
function scatterSize = getSizes(indx)
scatterSize = 9*(pfTracksRef{indx}(:)+1)-6;
end
end
Line 32:
pfTracksRef{pfTrackI}(end+1) = ~strcmp(get(source,'SelectionType'),'alt');
This is where the left or right mouse button is recorded, later used in getSizes(), with the output used for calls to scatter (or when setting the scatter series SizeData).
I can't figure out why the points created before a size change persist after deleteing the associated data and setting the scatter values to that data. It seems like the first time a size is changed a new scatter series is created.
Any light that could be shed on this issue would be appreciated, and any fixes would be doubly appreciated.
Thanks.
EDIT: One way to see what I'm talking about is to do the following:
Left click several times (lets say 5), spaced out so you can see them all.
Push Enter
Left click 5 more times, again spaced out, note they are in red (this is a desired functionality for my final product).
Press backspace until all the points are no longer visible. This is the correct funcitonality.
Incorrect:
Left click 1 time, right click 4 times.
Press enter
Left click 1 time, right click 4 times.
Backspace repeatedly (10 times). Note that the first 2 points (1 large and 1 small) from both the red and the yellow persist, even though you have clicked backspace enough times to delete everything, which is what happened when there was no size change.
EDIT: I've added code below that replicates the issue I am seeing using some loops and doing away with requiring mouse and keyboard input.
function ReplicateIssueWithLoops()
mainFig = figure;
imshow(zeros(512,512));
hold on;
axis image;
for hh = 1:2
%two runs, the first works fine, the second fails to remove scatter
%points from the figure.
cStrs = {'r','y'};
tracks = {[]};
tracksSizeRef = {[]};
trackI = 1;
scatters{1} = scatter([],[],5,cStrs{2});
for ii = 1:2
%simulates 2 sets
for jj = 1:5
%simulates 5 left clicks each
tracks{ii}(end+1,:) = [256,(ii*6+jj)*20];
if(hh ==1) %Simulates all left clicks
tracksSizeRef{ii}(end+1) = 12;
else %Simulates 1 left click and 4 right clicks
tracksSizeRef{ii}(end+1) = ((jj==1)+1)*9-6;
end
if(numel(scatters)<ii)
scatters{ii} = scatter(tracks{ii}(:,1),tracks{ii}(:,2),tracksSizeRef{ii},cStrs{mod(ii,2)+1});
else
set(scatters{ii},{'XData','YData','SizeData'},{tracks{ii}(:,1),tracks{ii}(:,2),tracksSizeRef{ii}});
end
pause(0.25);
end
tracks{ii+1}=[];
tracksSizeRef{ii+1} = [];
end
trackI = ii;
for kk = 1:10
%simulates 10 backspaces
if(isempty(tracks{trackI}) && numel(tracks)>1)
tracks(trackI) = [];
tracksSizeRef(trackI) = [];
if(numel(scatters) >= trackI)
scatters(trackI:end) = [];
end
trackI = trackI -1;
end
if(numel(tracks{trackI}))
tracks{trackI}(end,:) = [];
tracksSizeRef{trackI}(end) = [];
end
refreshScatters();
pause(0.25);
end
end
function refreshScatters()
for n = 1:numel(scatters)
if(numel(tracks{n})>0)
set(scatters{n},{'XData','YData','SizeData','MarkerEdgeColor'},{tracks{n}(:,1),tracks{n}(:,2),tracksSizeRef{n},cStrs{mod(n,2)+1}});
else
set(scatters{n},{'XData','YData'},{[],[]});
end
end
end
end
Below are pictures demonstrating how the issue looks on my end. In left to right order the pictures show 1) at the end of the first 10 points, 2) after deleting the first 10 points , 3) after the second ten points (the set with size difference), 4) after deleting the second 10 points (4 remain, the two from each set when the size was first changed).
I want to get a number in a gui (say N), and create N "edittext" in another gui figure when pushing a "pushbutton". I write this code to do so (which works correctly):
%%%%%%%%%%%%%%%%%%%%%%%%%%%
input_num = str2double(get(handles.edit1,'String'));
edittext = zeros(input_num,1);
panel= uipanel('parent',untitled1,...
'Title','Input Data',...
'position',[.01 .05 .25 .95]);
for i = 1:input_num
edittext(i,1) = uicontrol('parent',panel,'style','edit',...
'string','',...
'position',[20 360-i*25 40 20]);
end
%%%%%%%%%%%%%%%%%%%%%%
then in the second gui, I want to get the data which are entered by user in the created "edittext"s and use them to do something.
How can I do that?
You can do it using the Tag properties of the figures and uipanel you are creating to fetch the appropriate element from the 1st GUI in the 2nd GUI.
Here is a commented example:
Let's create GUI1 with the original tag 'GUI1'. Notice that we also add a tag to the uipanel...and that we use k as the loop variable instead of i.
function MakeEditTextsGUI
hFig1 = figure('Name','Fig1','Tag','GUI1');
input_num = 3;
edittext = zeros(input_num,1);
panel= uipanel('parent',hFig1,'Tag','Panel1',...
'Title','Input Data',...
'position',[.01 .05 .25 .95]);
for k = 1:input_num
edittext(k,1) = uicontrol('parent',panel,'style','edit',...
'string','','position',[20 360-k*25 40 20],'Tag',sprintf('edit_%i',k));
end
end
This is the figure in which the user enters the data.
Let's create a 2nd GUI with a pushbutton that the user pushes to fetch the data from the 1st GUI, which must be opened:
function GetEditBoxes
hFig2 = figure('Position',[400 400 200 100],'Tag','GUI2');
hButton = uicontrol('Style','push','Position',[60 60 80 20],'String','Get content','Callback',#(s,e) GetContent);
function GetContent
%// Fetch figure 1
hCurrFig = findobj('Tag','GUI1');
%// Make current figure
figure(hCurrFig);
%// Fetch Panel1
hCurrPanel = findobj('Tag','Panel1');
%// Get its children and order from first to last
EditBoxes = flipud(get(hCurrPanel,'Children'));
%// Get number of edit boxes
NumBoxes = numel(EditBoxes);
%// Go and get the data!
for p = 1:NumBoxes
get(EditBoxes(p),'String')
end
end
end
I did not include any error catching code but its easy to verify if every edit box contains an entry. Once the user presses the button, the content of each box is displayed in the command window but you can easily store them into an array or something.
Have fun!
I have a simple, sort of GUI code shown below.
This "test_keypress" function creates a figure, and it responses to the keypress (space).
Now, I want to add a constraint so that Matlab accepts only one keypress for a certain period of time (say, 1 second).
In other words, I want to reject a keypress if it happens within 1 sec after the previous keypress occurred.
How can I do that?
function test_keypress
f = figure;
set(f,'KeyPressFcn',#figInput);
imshow(ones(200,200,3));
end
function figInput(src,evnt)
if strcmp(evnt.Key,'space')
imshow(rand(200,200,3));
end
end
You can store the current time, and only evaluate the imshow command if the key-press occurs at least 100 seconds after the last one.
function test_keypress
f = figure;
set(f,'KeyPressFcn',#figInput);
imshow(ones(200,200,3));
%# initialize time to "now"
set(f,'userData',clock);
end
function figInput(src,evnt)
currentTime = clock;
%# get last time
lastTime = get(src,'UserData');
%# show new figure if the right key has been pressed and at least
%# 100 seconds have elapsed
if strcmp(evnt.Key,'space') && etime(lastTime,currentTime) > 100
imshow(rand(200,200,3));
%# also update time
set(src,'UserData',currentTime)
end
end