我有这个程序,如您所见,它从目录中提取随机图片,并要求用户进行比较。使用滑块设置值后,用户按下“下一次试用”按钮,重置滑块和随机图片对。如何修改代码,以便在重复一定次数(按下按钮)后,程序自动结束(最好带有“实验结束”消息)?
我在 MATLAB 文档中找不到有关如何执行此操作的任何信息。我是否需要设置一个变量,以便每次按下按钮时都会将“1”添加到变量的值中,以便当它达到某个数字(例如“100”)时终止?这是最简单的方法吗?
这是脚本:
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