How Do I Get My Script To Work For Just One Figure Window? Currently It Loads Into Two

4 views (last 30 days)
Hi, I'm trying to write a script for a d1git span experiment. In the digit span experiment, participants view a sequence of numbers (N) of a given length (L) for time (T) and are then immediately asked to type in the number sequence (R). I want to give the participant instructions and then ask the participant to determine the length of the digit sequence (L) and the stimulus duration (T). Then after that, I want to generate the random sequence (N) and display the sequence for time (T). Finally, I want to get the participant response (R). After this, I'm going to save out all the variables.
I want the script to roll into one figure window, but currently, its loading into two. This is the script:
function [N, L, T, R] = digitSpanExperiment()
% Create figure window
f = figure('Name','Digit Span Experiment','NumberTitle','off','Position',[400 400 300 150]);
% Create text instructions
uicontrol('Style','text','String','Please enter the number of digits and the exposure time:','Position',[20 120 250 20]);
% Create edit boxes to input the number of digits (L) and the exposure time (T)
uicontrol('Style','edit','String','','Position',[80 80 40 20],'Callback',@setL);
uicontrol('Style','edit','String','','Position',[160 80 40 20],'Callback',@setT);
% Create push button to start experiment
uicontrol('Style','pushbutton','String','Start','Position',[120 40 60 20],'Callback',@startExperiment);
function setL(source,eventdata)
L = str2double(get(source,'String'));
end
function setT(source,eventdata)
T = str2double(get(source,'String'));
end
function startExperiment(source,eventdata)
delete(f);
N = randi(9,1,L);
figure('Name','Digit Span Experiment','NumberTitle','off','Position',[400 400 300 300]);
uicontrol('Style','text','String',num2str(N),'Position',[120 130 60 20]);
pause(T);
figure('Name','Digit Span Experiment','NumberTitle','off','Position',[400 400 300 150]);
uicontrol('Style','text','String','Please enter the number sequence:','Position',[20 120 250 20]);
uicontrol('Style','edit','String','','Position',[80 80 120 20],'Callback',@setR);
uicontrol('Style','pushbutton','String','Submit','Position',[120 40 60 20],'Callback',@finishExperiment);
end
function setR(source,eventdata)
R = str2double(get(source,'String'));
end
function finishExperiment(source,eventdata)
delete(f);
save('digitSpanExperiment','N','L','T','R');
end
end
The problem is, I want the entire script to work for just one figure window. Currently, my script creates a figure window, deletes that figure window, and then it creates another figure window. What I want it to do is to create just one universal figure window that runs the entire experiment. Currently that's not the case so if someone can help me set that up, I would really appreciate it.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2022
You are using shared variables that are being examined or set within callbacks set on the graphics objects.
Now, when you create graphics objects, they persist when the function returns, and can be interacted with. But the shared variables used in digitSpanExperiment do not persist once digitSpanExperiment returns. So if you interact with the shared variables after digitSpanExperiment returns (because the graphics objects still exist and still have those callbacks) then you have problems.
You can get around that problem with f by making f persistent -- but L and T are return variables from digitSpanExperiment and so cannot be made persistent. You cannot change what a function returns after the function has returned.
Or.... you could use uiwait or waitfor so that the digitSpanExperiment function does not return until the user clicked the Submit button.

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!