Question

I have this program, which as you can see is pulling random pictures out of a directory, and asking the user to compare them. After setting the value with the slider, the user presses a "Next Trial" button, which resets the slider and the random picture pair. How do I modify the code so that, after a certain number of repetitions (button presses), the program automatically ends (preferably with a "Experiment Ended" message)?

I can't find anything about how to do this in the MATLAB documentation. Do I need to set a variable, so that everytime the button is pressed "1" is added to the value of the variable, so that when it reaches a certain number (say "100") it terminates? Is that the easiest way to do this?

Here's the script:

function trials

files = dir(fullfile('samples','*.png'));
nFiles = numel(files);
combos = nchoosek(1:nFiles, 2);
index = combos(randperm(size(combos, 1)), :);
picture1 = files(index(1)).name;
picture2 = files(index(2)).name;
image1 = fullfile('samples',picture1);
image2 = fullfile('samples',picture2);
subplot(1,2,1); imshow(image1);
subplot(1,2,2); imshow(image2);

uicontrol('Style', 'text',...
        'Position', [200 375 200 20],...
        'String','How related are these pictures?');
uicontrol('Style', 'text',...
        'Position', [50 375 100 20],...
        'String','Unrelated');
uicontrol('Style', 'text',...
        'Position', [450 375 100 20],...
        'String','Closely related');
uicontrol('Style','pushbutton','String','Next Trial',...
        'Position', [250 45 100 20],...
        'Callback','clf; trials()');

h = uicontrol(gcf,...
   'Style','slider',...
   'Min' ,0,'Max',50, ...
   'Position',[100 350 400 20], ...
   'Value', 25,...
   'SliderStep',[0.02 0.1], ...
   'BackgroundColor',[0.8,0.8,0.8]);

set(gcf, 'WindowButtonMotionFcn', @cb);

lastVal = get(h, 'Value'); 

function cb(s,e)
    if get(h, 'Value') ~= lastVal 
    lastVal = get(h, 'Value'); 
    fprintf('Slider value: %f\n', lastVal); 
    end
end

end
Was it helpful?

Solution

One problem I see here is that the callback for your "Next Trial" button simply calls the function trials again. This is going to generate the combinations of images again, which you only want/need to do once. You should set the callback to be another nested function (like cb) so it can access the already-generated combinations.

Another problem is how you initialize picture1 and picture2. You should do your indexing like so:

picture1 = files(index(1,1)).name;  %# Note that index is 2-dimensional!
picture2 = files(index(1,2)).name;

Now, you'll first want to initialize a variable to track the number of trials inside the function trials, as well as a maximum number of trials:

nReps = 1;
maxReps = 100;

Then your "Next Trial" button callback would look something like this:

function newTrial(s, e)
    %# I assume you need the slider value for each trial, so fetch it
    %#   and save/store it here.

    %# Check the number of trials:
    if (nReps == maxReps)
        close(gcf);  %# Close the figure window
    else
        nReps = nReps + 1;
    end

    %# Get the new images:
    picture1 = files(index(nReps, 1)).name;
    picture2 = files(index(nReps, 2)).name;
    image1 = fullfile('samples', picture1);
    image2 = fullfile('samples', picture2);

    %# Plot the new images:
    subplot(1,2,1);
    imshow(image1);
    subplot(1,2,2);
    imshow(image2);

    %# Reset the slider to the default value:
    set(h, 'Value', 25);
end


One additional suggestion... instead of displaying the slider value on the screen using FPRINTF, I would create a text object in your GUI and simply update its string value:

hText = uicontrol('Style', 'text', ...
                  'String', 'Slider value: 25', ... );

%# And in function cb...
set(hText, 'String', sprintf('Slider value: %f', lastVal));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top