Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:49:57+00:00 2026-05-30T12:49:57+00:00

I have this program, which as you can see is pulling random pictures out

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:49:59+00:00Added an answer on May 30, 2026 at 12:49 pm

    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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping someone can shed some light on this. I have a PHP program which
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
Consider this problem: I have a program which should fetch (let's say) 100 records
I have a program which does a system call: latex somefile.latex This runs ok,
I have a GUI program, which would call a cmd in this GUI program.
I have a simple script which is used to start another program. This other
I have a c# program which throws a NullReferenceException() . When I start this
I have a legacy VB6 program which installs an Access file in a sub-directory
I have a program which makes some calculations according to user data. The program
I have this program in c++: #include <iostream> using namespace std; int main() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.