2

私はこのプログラムを持っています。これは、ご覧のとおり、ディレクトリからランダムな画像を引き出し、ユーザーにそれらを比較するように依頼しています。スライダーで値を設定した後、ユーザーは「次の試行」ボタンを押します。これにより、スライダーとランダムな画像のペアがリセットされます。特定の回数の繰り返し(ボタンを押す)の後、プログラムが自動的に終了するようにコードを変更するにはどうすればよいですか(できれば「実験が終了しました」というメッセージが表示されます)。

これを行う方法については、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
4

1 に答える 1

3

ここで私が見ている問題の1つは、「次の試行」ボタンのコールバックが単に関数trialsを再度呼び出すことです。これにより、画像の組み合わせが再度生成されます。これは、1回だけ実行する必要があります。cbすでに生成された組み合わせにアクセスできるように、コールバックを別のネストされた関数(のような)に設定する必要があります。

もう1つの問題は、とを初期化する方法picture1ですpicture2。次のようにインデックスを作成する必要があります。

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));
于 2012-02-22T16:51:59.663 に答える