【问题标题】:MATLAB slider value to data fileMATLAB 滑块值到数据文件
【发布时间】: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 保存在数据文件中的方式。问题是双重的:

  1. 如果不移动滑块,则不会保存任何值。因此,如果它留在起始位置 (25),并且单击“下一次试验”,它会忽略该试验,就好像它没有发生一样。我不想要这个。我希望它保存滑块位置的值,无论它是否被移动。

  2. 数据文件如下所示:

    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

如何让它在试用结束时只打印滑块的最后一个位置(如果滑块不移动,包括默认值)?

【问题讨论】:

    标签: image matlab file slider


    【解决方案1】:

    将文件编写代码放在按钮回调(@NextTrial)而不是滑块回调(@cb)中。

    【讨论】:

    • 一开始没用,但我想通了。谢谢! (出于某种原因,我不得不摆脱其他一些干扰@NextTrial 的东西)
    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2013-11-02
    相关资源
    最近更新 更多