1

ユーザーが値を入力し、プッシュボタンを押すと外部関数が実行され、エラー メッセージが表示される GUI を作成しています。GUI コーディングで変数を正常に挿入できません。変数を挿入する場所について混乱しています。ハンドルを試しましたが、残念ながら機能しません。

  % --- Executes just before Stallfunction is made visible.
  function Stallfunction_OpeningFcn(hObject, ~, handles, varargin)
  % This function has no output args, see OutputFcn.
  % hObject    handle to figure
  % eventdata  reserved - to be defined in a future version of MATLAB
  % handles    structure with handles and user data (see GUIDATA)
  % varargin   command line arguments to Stallfunction (see VARARGIN)
  % Choose default command line output for Stallfunction
   handles.user_entry = user_entry;
  % Update handles structure
  guidata(hObject, handles);
  % UIWAIT makes Stallfunction wait for user response (see UIRESUME)
  % uiwait(handles.figure1);

上記のコードに変数を挿入しましたが、'user_entry' は正しいですか?

4

2 に答える 2

1

user_entry関数で値が割り当てられていません。次のように値を渡して GUI を起動するとuser_entry:

Stallfunction(user_entry)

その場合、openingFcn のコードの最初の行は次のようになります。

if ~isempty(varargin)
    user_entry = varargin{1};
else
    error('please start the GUI with an input value')
end

user_entryこの後、すでに行っているように、handles 構造に割り当てることができます。

于 2013-03-10T18:27:51.530 に答える
0

これを試して:

function num = get_num()
    fig = figure('Units', 'characters', ...
                 'Position', [70 20 30 5], ...
                 'CloseRequestFcn', @close_Callback);

    edit_num = uicontrol(...
                'Parent', fig, ...
                'Style', 'edit', ...
                'Units', 'characters', ...
                'Position', [1 1 10 3], ...
                'HorizontalAlignment', 'left', ...            
                'String', 'init', ...
                'Callback', @edit_num_Callback);  

    button_finish = uicontrol( ...
        'Parent', fig, ...
        'Tag', 'button_finish', ...
        'Style', 'pushbutton', ...
        'Units', 'characters', ...
        'Position', [15 1 10 3], ...
        'String', 'Finish', ...
        'Callback', @button_finish_Callback);            

    % Nested functions
    function edit_num_Callback(hObject,eventdata)
        disp('this is a callback for edit box');
    end            

    function button_finish_Callback(hObject,eventdata) 
        % Exit
        close(fig);
    end       

    function  close_Callback(hObject,eventdata)
        num_prelim = str2num(get(edit_num,'string'));
        if(isempty(num_prelim))
            errordlg('Must be a number.','Error','modal');
            return;
        end
        num = num_prelim;
        delete(fig);
    end

waitfor(fig);  
end

あなたがこれを台無しにして、あなたが望むものを手に入れることができるかどうか見てください。また、ネストされた関数の使用方法と、MATLABでのコールバックの動作についても学びます。これを関数ファイルとして保存してから、「num=getnum;」を呼び出します。

于 2013-03-11T00:31:49.347 に答える