2012-03-12 20 views
0

我有以下的MATLAB代碼: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 

我怎麼得到它只能打印滑塊的最後位置在試驗結束時(包括默認值,如果滑塊不移動)?

回答

2

將文件寫入代碼的按鈕回調(@NextTrial)而不是滑塊回調(@cb)。

+0

它一開始並沒有工作,但我想通了。謝謝! (由於某些原因,我必須擺脫一些干擾@NextTrial的其他東西) – 2012-03-13 14:02:37