MATLAB 在 X 次重复后停止程序?

发布于 2025-01-08 06:14:51 字数 1523 浏览 0 评论 0原文

我有这个程序,如您所见,它从目录中提取随机图片,并要求用户比较它们。在使用滑块设置值之后,用户按下“下一次尝试”按钮,这将重置滑块和随机图片对。如何修改代码,以便在重复一定次数(按下按钮)后,程序自动结束(最好显示“实验结束”消息)?

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2025-01-15 06:14:51

我在这里看到的一个问题是“下一次试用”按钮的回调只是再次调用函数 trials 。这将再次生成图像组合,您只想/需要执行一次。您应该将回调设置为另一个嵌套函数(例如cb),以便它可以访问已经生成的组合。

另一个问题是如何初始化 picture1picture2。您应该像这样进行索引:

picture1 = files(index(1,1)).name;  %# Note that index is 2-dimensional!
picture2 = files(index(1,2)).name;

现在,您首先要初始化一个变量来跟踪函数 trials 内的试验次数,以及最大试验次数:

nReps = 1;
maxReps = 100;

然后您的“Next试用”按钮回调看起来像这样:

function newTrial(s, e)
    %# I assume you need the slider value for each trial, so fetch it
    %#   and save/store it here.

    %# Check the number of trials:
    if (nReps == maxReps)
        close(gcf);  %# Close the figure window
    else
        nReps = nReps + 1;
    end

    %# Get the new images:
    picture1 = files(index(nReps, 1)).name;
    picture2 = files(index(nReps, 2)).name;
    image1 = fullfile('samples', picture1);
    image2 = fullfile('samples', picture2);

    %# Plot the new images:
    subplot(1,2,1);
    imshow(image1);
    subplot(1,2,2);
    imshow(image2);

    %# Reset the slider to the default value:
    set(h, 'Value', 25);
end

另一项建议...不要使用 FPRINTF,我将在 GUI 中创建一个文本对象并简单地更新其字符串值:

hText = uicontrol('Style', 'text', ...
                  'String', 'Slider value: 25', ... );

%# And in function cb...
set(hText, 'String', sprintf('Slider value: %f', lastVal));

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 (like cb) so it can access the already-generated combinations.

Another problem is how you initialize picture1 and picture2. You should do your indexing like so:

picture1 = files(index(1,1)).name;  %# Note that index is 2-dimensional!
picture2 = files(index(1,2)).name;

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:

nReps = 1;
maxReps = 100;

Then your "Next Trial" button callback would look something like this:

function newTrial(s, e)
    %# I assume you need the slider value for each trial, so fetch it
    %#   and save/store it here.

    %# Check the number of trials:
    if (nReps == maxReps)
        close(gcf);  %# Close the figure window
    else
        nReps = nReps + 1;
    end

    %# Get the new images:
    picture1 = files(index(nReps, 1)).name;
    picture2 = files(index(nReps, 2)).name;
    image1 = fullfile('samples', picture1);
    image2 = fullfile('samples', picture2);

    %# Plot the new images:
    subplot(1,2,1);
    imshow(image1);
    subplot(1,2,2);
    imshow(image2);

    %# Reset the slider to the default value:
    set(h, 'Value', 25);
end

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:

hText = uicontrol('Style', 'text', ...
                  'String', 'Slider value: 25', ... );

%# And in function cb...
set(hText, 'String', sprintf('Slider value: %f', lastVal));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文