2012-02-22 5 views
2

나는 당신이 볼 수 있듯이 임의의 그림을 디렉토리에서 꺼내 사용자가 비교하도록 요구하는이 프로그램을 가지고있다. 슬라이더로 값을 설정 한 후 사용자는 슬라이더와 임의의 그림 쌍을 재설정하는 "다음 평가판"버튼을 누릅니다. 특정 횟수의 반복 (버튼 누름) 후 프로그램이 자동으로 종료되도록 코드를 수정하려면 어떻게해야합니까? (가급적이면 "실험 종료 됨"메시지 사용)?X 반복 후 MATLAB 프로그램을 중지 하시겠습니까?

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 
+1

당신이 다시 구현하려고를 (http://en.wikipedia.org/wiki/Hot_or_Not)? – yuk

답변

3

하나에 문제가 "다음 시험"버튼에 대한 콜백 단순히 다시 기능 trials를 호출하는 것입니다. 이렇게하면 이미지 조합을 다시 생성 할 수 있습니다. 단 한번만 수행하고 싶을뿐입니다. 이미 생성 된 조합에 액세스 할 수 있도록 콜백을 다른 중첩 함수 (예 : cb)로 설정해야합니다.

또 다른 문제는 picture1picture2을 초기화하는 방법입니다. 당신은 너무처럼 인덱싱을 수행해야합니다

nReps = 1; 
maxReps = 100; 
:

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

을 지금, 당신은 먼저 기능 trials 내부 시험의 수뿐만 아니라 시험의 최대 수를 추적하는 변수를 초기화 할 수 있습니다 이 같은

그런 다음 "다음 시험"버튼 콜백 보면 무언가 : 추가

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)); 
+0

감사합니다. (실제로 슬라이더 값 부분을 변경했는데 이전 버전의 코드를 실수로 게시했습니다.) 어쨌든, 좋은 제안, 나는 그들이 어떻게 작동하는지 보게 될 것입니다. –