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

  • SEARCH
  • Home
  • 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 7632457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:37:49+00:00 2026-05-31T06:37:49+00:00

I have the following MATLAB code: function START_Callback(a,b) global h; global lastVal; global picture1;

  • 0

I have the following MATLAB code:

function START_Callback(a,b)
global h;
global lastVal;
global picture1;
global picture2;
global nRep;
global MaxRep;
global index;
global files;
delete(gcf);

nRep = 1;
MaxRep = 529;

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

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

h = uicontrol(gcf,...
   'Style','slider',...
   'Min' ,0,'Max',50, ...
   'Position',[100 20 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)
global h
global lastVal;
global picture1;
global picture2;
global fileout;
global SaveResults;

fid = fopen(fileout, 'a');
    if get(h, 'Value') ~= lastVal;
        lastVal = get(h, 'Value');
        if SaveResults > 0;
        fprintf(fid, '%s\t%s\t%f\n', picture1, picture2, lastVal);
        fclose(fid);
        else
        fclose(fid);
    end
end

My problem is with the way lastVal is saved in the datafile. The problem is twofold:

  1. If the slider isn’t moved, no value is saved. So if it is left in the starting position (25), and “Next Trial” is clicked, it ignores that trial as if it hadn’t happened. I don’t want this. I want it to save whatever the value of the slider position is, regardless of whether it was moved or not.

  2. The datafile looks like this:

    monkey.png  ostrich.png 24.262537
    monkey.png  ostrich.png 23.082596
    monkey.png  ostrich.png 20.870207
    monkey.png  ostrich.png 17.772862
    monkey.png  ostrich.png 13.790561
    monkey.png  ostrich.png 9.218289
    monkey.png  ostrich.png 5.383481
    monkey.png  ostrich.png 3.023599
    monkey.png  ostrich.png 2.433628
    

That is, MATLAB isn’t just saving the LAST position of the slider when it is moved, it is saving all intermediate values as well. So I want the above to only be:

monkey.png  ostrich.png     2.433628

How do I get it to ONLY print the last position of the slider at the end of the trial (including the default value if the slider is not moved)?

  • 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-31T06:37:51+00:00Added an answer on May 31, 2026 at 6:37 am

    Put the file writing code in the button callback (@NextTrial) not the slider callback (@cb).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following matlab code: randarray = gpuArray(rand(N,1)); N = 1000; tic g=0;
suppose that there is given following matlab code function dp = derp(p) n =
For example if I have the following code in MATLAB x = 0:0.1:2*pi; y
I have written the following code in MATLAB to process large images of the
I have the following code from mathematica and trying to do it with matlab
hi currently i have the following code in my matlab values = [0;1;0;0;1;0;1;0]; %
I have created a binary file using the following matlab code: x is an
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
What is equivalent pythonic implementation for the following simple piece of code in Matlab.

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.