MATLAB 在 X 次重复后停止程序?
我有这个程序,如您所见,它从目录中提取随机图片,并要求用户比较它们。在使用滑块设置值之后,用户按下“下一次尝试”按钮,这将重置滑块和随机图片对。如何修改代码,以便在重复一定次数(按下按钮)后,程序自动结束(最好显示“实验结束”消息)?
我在 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里看到的一个问题是“下一次试用”按钮的回调只是再次调用函数
trials
。这将再次生成图像组合,您只想/需要执行一次。您应该将回调设置为另一个嵌套函数(例如cb
),以便它可以访问已经生成的组合。另一个问题是如何初始化
picture1
和picture2
。您应该像这样进行索引:现在,您首先要初始化一个变量来跟踪函数
trials
内的试验次数,以及最大试验次数:然后您的“Next试用”按钮回调看起来像这样:
另一项建议...不要使用 FPRINTF,我将在 GUI 中创建一个文本对象并简单地更新其字符串值:
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 (likecb
) so it can access the already-generated combinations.Another problem is how you initialize
picture1
andpicture2
. You should do your indexing like so: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:Then your "Next Trial" button callback would look something like this:
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: