0

@Amroが提案したことを試していますが、コードの一部を変更したところ、いくつかのエラーが発生しました:/

私を助けてください。

オプションを次のように設定しました。

options = {
    {'r','g','b','c','m','y','k'} ;
    {'x','o','.','s'} ;
    {'1','2','3'} ;
    {'2','3'} ;
    {'1','3'} ;
    {'1','2'}
};

今、私はmatlabで書いた:

mainGUI(options)

これらのエラーが発生しました:

??? Index exceeds matrix dimensions.

Error in ==> tmp>secondaryGUI at 67
        if strcmp(selected{i},options{i}{j})

Error in ==> tmp>callback at 17
    hOptsGUI = secondaryGUI(hFig);

??? Error while evaluating uicontrol Callback

これはコードです:

function  mainGUI(options)
    %# current options
    opts = {'r', '.', '1'};

    %# create main figure, with plot and options button
    hFig = figure;
    callback

    %# options button callback function
    function callback(o,e)
        %# save current options (sharing data between the two GUIs)
        setappdata(hFig, 'opts',opts);

        %# display options dialog and wait for it
        hOptsGUI = secondaryGUI(hFig, options);
        waitfor(hOptsGUI);

        %# get new options, and update plot accordingly
        opts = getappdata(hFig, 'opts');
        opts
    end
end

function hFig = secondaryGUI(hParentFig, options)
    %# create figure
    hFig = figure('Menubar','none', 'Resize','off', ...
    'WindowStyle','modal', 'Position',[100 100 350 200]);
    movegui(hFig, 'center');

    %# all possible plot options

    options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
    num = length(options);

    %# get saved settings
    selected = getappdata(hParentFig, 'opts');

    %# top/bottom panels
    hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
    'Units','normalized', 'Position',[0 0.0 1 0.2]);
    hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
    'Units','normalized', 'Position',[0 0.2 1 0.8]);

    %# buttongroups in top panel
    hBtnGrp = zeros(1,num);
    width = 1/num;
    for i=1:num
        %# create button group
        hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
            'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
        %# populate it with radio buttons
        height = 1./numel(options{i});
        for j=1:numel(options{i})
            h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
            'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
            'String',options{i}{j});
            %# set initially selected values
            if strcmp(selected{i},options{i}{j})
                set(hBtnGrp(i), 'SelectedObject',h)
            end
        end
    end

    %# save button in bottom panel
    uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
        'Units','normalized', 'Position',[0.3 0.2 0.4 0.6], ...
        'String','start', 'Callback',@callback)

    %# save button callback function
    function callback(o,e)
        %# get selected values
        hObjs = get(hBtnGrp(:), 'SelectedObject');
        vals = get(cell2mat(hObjs),{'String'});

        %# update settings
        setappdata(hParentFig, 'opts',vals);

        %# close options dialog
        close(hFig)
    end
end

{'r', '.', '1'} の値がないため、変数 'opts' に問題がある可能性があります。

マトリックスの次元を超える理由がわからないので、オプションの長さは 6 です。

ありがとう!

4

1 に答える 1

1

私がこれを正しく理解していれば、ユーザーがいくつかのパラメーターを設定し、選択したオプションを返すことを可能にする GUI を設計していることになります。

現在行っているようにすぐに値を返す代わりに、データ共有メカニズムとしてGETAPPDATA / SETAPPDATAを使用できます。

レイアウトに関しては、パネルを使用してコンポーネントをグループ化します。これにより、より柔軟な GUI が可能になります。

説明するためのサンプル アプリケーションを次に示します。アイデアは、プロットを含む主要な図があり、プロット オプションをカスタマイズするための 2 番目の「ダイアログ」を提供するというものです。

function mainGUI()
    %# current options
    opts = {'r', '.', '1'};

    %# create main figure, with plot and options button
    hFig = figure;
    hLine = plot(cumsum(rand(100,1)-0.5), ...
        'Color',opts{1}, 'Marker',opts{2}, 'LineWidth',str2double(opts{3}));
    uicontrol('Style','pushbutton', 'String','Options...', 'Callback',@callback)

    %# options button callback function
    function callback(o,e)
        %# save current options (sharing data between the two GUIs)
        setappdata(hFig, 'opts',opts);

        %# display options dialog and wait for it
        hOptsGUI = secondaryGUI(hFig);
        waitfor(hOptsGUI);

        %# get new options, and update plot accordingly
        opts = getappdata(hFig, 'opts');
        set(hLine, 'Color',opts{1}, 'Marker',opts{2}, 'LineWidth',str2double(opts{3}))
    end
end

function hFig = secondaryGUI(hParentFig)
    %# create figure
    hFig = figure('Menubar','none', 'Resize','off', ...
        'WindowStyle','modal', 'Position',[100 100 350 200]);
    movegui(hFig, 'center');

    %# all possible plot options
    options = {
        {'r','g','b','c','m','y','k'} ;    %# color
        {'x','o','.','s'} ;                %# shape
        {'1','2','3'}                      %# width
    };
    options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
    num = length(options);

    %# get saved settings
    selected = getappdata(hParentFig, 'opts');

    %# top/bottom panels
    hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
        'Units','normalized', 'Position',[0 0.0 1 0.2]);
    hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
        'Units','normalized', 'Position',[0 0.2 1 0.8]);

    %# buttongroups in top panel
    hBtnGrp = zeros(1,num);
    width = 1/num;
    for i=1:num
        %# create button group
        hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
            'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
        %# populate it with radio buttons
        height = 1./numel(options{i});
        for j=1:numel(options{i})
            h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
                'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
                'String',options{i}{j});
            %# set initially selected values
            if strcmp(selected{i},options{i}{j})
                set(hBtnGrp(i), 'SelectedObject',h)
            end
        end
    end

    %# save button in bottom panel
    uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
        'Units','normalized', 'Position',[0.3 0.2 0.4 0.6], ...
        'String','Save & Close', 'Callback',@callback)

    %# save button callback function
    function callback(o,e)
        %# get selected values
        hObjs = get(hBtnGrp(:), 'SelectedObject');
        vals = get(cell2mat(hObjs),{'String'});

        %# update settings
        setappdata(hParentFig, 'opts',vals);

        %# close options dialog
        close(hFig)
    end
end

スクリーンショット

于 2012-05-27T00:22:29.397 に答える