Reputation: 883
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:
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.
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)?
Upvotes: 0
Views: 357
Reputation: 283971
Put the file writing code in the button callback (@NextTrial
) not the slider callback (@cb
).
Upvotes: 2