【发布时间】:2012-03-12 15:18:22
【问题描述】:
我有以下 MATLAB 代码:
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
我的问题是 lastVal 保存在数据文件中的方式。问题是双重的:
如果不移动滑块,则不会保存任何值。因此,如果它留在起始位置 (25),并且单击“下一次试验”,它会忽略该试验,就好像它没有发生一样。我不想要这个。我希望它保存滑块位置的值,无论它是否被移动。
-
数据文件如下所示:
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
也就是说,MATLAB 不仅保存滑块移动时的最后一个位置,还保存所有中间值。所以我只希望上面的内容是:
monkey.png ostrich.png 2.433628
如何让它在试用结束时只打印滑块的最后一个位置(如果滑块不移动,包括默认值)?
【问题讨论】: