2013-03-10 2 views
1

사용자가 값을 입력하는 GUI를 만들고 푸시 버튼을 누르면 외부 함수가 실행되고 오류 메시지가 표시됩니다. 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); 

I '는이 user_entry'는 상기 코드에서 변수를 삽입 그 맞습니까?

답변

1

user_entry에는 함수에 값이 할당되지 않았습니다. 이 같은 user_entry에 대한 값을 전달하여 GUI를 실행하는 경우 :

Stallfunction(user_entry) 

다음 openingFcn에서 코드의 첫 번째 라인은 다음과 같아야합니다

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

이 후, 당신은에 user_entry을 할당 할 수 있습니다 이미하고있는 것처럼 구조를 처리합니다.

+0

값을 전달하여 GUI가 시작되지 않습니다. GUI 인터페이스가 있고 편집 상자에 값을 입력하고 푸시 버튼을 눌러 다른 기능을 실행합니다. 당신이 말하는 것과 똑같은가요? – user1860036

+0

@ user1860036 :이 경우 상자의 내용을'handles.user_entry'에 쓸 수 있도록 editbox의 콜백을 작성해야합니다. – Jonas

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;"을 호출하십시오.

관련 문제