2012-02-22 18 views
2

私はあなたが見ることができるように、ディレクトリからランダムな画像を引っ張って、それらを比較するように頼んでいるこのプログラムを持っています。スライダで値を設定した後、ユーザはスライダとランダムなピクチャのペアをリセットする "次の試行"ボタンを押します。特定の繰り返し回数(ボタンが押された後)でプログラムが自動的に終了するようにコードを変更するにはどうすればよいですか?(できれば "Experiment Ended"メッセージが表示されます)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://ja.wikipedia.org/wiki/Hot_or_Not)? – yuk

答えて

3

一つの問題は、あなたの「次の試行」ボタンのコールバックは、単に再び機能trialsを呼び出すことです。これは、画像の組み合わせを再度生成することになります。これは、一度しか行いたくない画像です。すでに生成された組み合わせにアクセスできるように、コールバックを別の入れ子関数(cbなど)に設定する必要があります。

もう1つの問題は、picture1picture2の初期化方法です。あなたはそのようなあなたのインデックス作成を行う必要があります。

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

、あなたが最初の関数trials内部の試行回数だけでなく、裁判の最大数を追跡するための変数を初期化したいと思う:

nReps = 1; 
maxReps = 100; 

次に、あなたの「次の試行」ボタンのコールバックは、次のようなものになります。

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 


1つの追加を提案...代わりにFPRINTFを使用して、画面上のスライダーの値を表示するので、私はあなたのGUIでテキストオブジェクトを作成し、単にその文字列値を更新します:

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

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

ありがとう! (私は実際にスライダーの値の部分を変更しました。私は誤って古いコードを投稿しました。とにかく、素晴らしい提案、私は彼らがどのように動作するかを見ていきます。 –

関連する問題