2017-11-12 1 views
1

누구나 내 기본 앱을 열어서 일부 값을 캡처 한 다음 내 기본 앱으로 다시 보내는 보조 앱을 만들 수 있습니까?두 앱 사이에서 정보 공유

이 문제는 app designer documentation에 처리되었음을 알고 있지만 해당 단계를 성공적으로 구현할 수 없습니다. 또한 예제를 실행하려고했지만 Matlab은 파일이 존재하지 않는다고 말합니다. 누구든지이 예제를 공유하면 도움이 될 것입니다.

답변

1

나는 이것을 독자적으로 구현하려하지 않았지만, 복잡한 애플 리케이션 아키텍처에 직면 할 때 이것을 어떻게 달성 할 수 있었는지 자주 궁금해했다.

실제로 동일한 스크립트/함수에서 두 개의 GUI를 인스턴스화하거나 하나의 GUI에서 해당 기능 중 하나에 다른 GUI를 만드는 경우 가장 간단한 방법은 play with function handles입니다. 예를 들어, 첫 번째 GUI는 해당 함수들 사이에 정의 된 함수 핸들을 대상 GUI의 생성자에 전달할 수 있습니다. 이렇게하면 대상 GUI가 필요할 때 첫 번째 GUI의 데이터 및/또는 속성을 수정하기 위해이를 호출 할 수 있습니다.

어쨌든 모범 사례로 간주되는 표준 방식은 다음과 같이 작동합니다. G1G2이라는 두 개의 GUI가 있고 그것들이 구별되어 있다고 가정 해 봅니다 (동일한 GUI의 두 인스턴스를 실행하지 않음). 둘 다 표시되는 경우 (HandleVisibilityon)이 둘 모두 Tag 식별자가 정의되어 있으면 (예 : G1G2) Matlab의 "작업 영역"에서 검색 할 수 있습니다. 따라서 :

% This is a G2 event handler 
function pushbutton1_Callback(hObject, eventdata, handles) 
    g1_h = findobj('Tag','G1'); 

    if (~isempty(g1_h)) 
     % get all data associated to G1 
     g1_data = guidata(g1_h); 

     % modify a G2 object based on a G1 object 
     set(handles.MyTextBox,'String',get(g1_data.MyEditBox,'String')); 
    end 
end 
0

MATLAB의 App Designer는 GUIDE의 기능 기반 GUI 대신 클래스 기반 GUI를 생성합니다. 이 접근 방식의 장점은 함수 반환이나 태그로 객체를 검색하는 등 창의적으로 작업하지 않고 객체로 GUI를 전달할 수 있다는 것입니다.

다음은이 개념에 대한 한 가지 접근법을 보여주는 간단한 프로그래밍 방식의 예입니다. 주 그림 창은 두 개의 입력을 제공하는 보조 프롬프트 창을 엽니 다. 프롬프트 창이 닫히면 1 차 GUI가 입력 값을 명령 창에 인쇄하고 종료합니다.

메인 창

classdef mainwindow < handle 
    properties 
     mainfig 
     butt 
    end 

    methods 
     function [self] = mainwindow() 
      % Build a GUI 
      self.mainfig = figure('Name', 'MainWindow', 'Numbertitle', 'off', ... 
            'MenuBar', 'none', 'ToolBar', 'none'); 
      self.butt = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ... 
            'Units', 'Normalized', 'Position', [0.1 0.1 0.8 0.8], ... 
            'String', 'Push Me', 'Callback', @(h,e) self.buttoncallback); 
     end 

     function buttoncallback(self) 
      tmpwindow = subwindow(); % Open popupwindow 
      uiwait(tmpwindow.mainfig); % Wait for popup window to be closed 
      fprintf('Parameter 1: %u\nParameter 2: %u\n', tmpwindow.parameter1, tmpwindow.parameter2); 

      close(self.mainfig); 
     end 
    end 
end 

서브 창

classdef subwindow < handle 
    properties 
     mainfig 
     label1 
     box1 
     label2 
     box2 
     closebutton 

     parameter1 
     parameter2 
    end 

    methods 
     function [self] = subwindow() 
      % Build a GUI 
      self.mainfig = figure('Name', 'SubWindow', 'Numbertitle', 'off', ... 
            'MenuBar', 'none', 'ToolBar', 'none'); 
      self.label1 = uicontrol('Parent', self.mainfig, 'Style', 'text', ... 
            'Units', 'Normalized', 'Position', [0.4 0.7 0.2 0.05], ... 
            'String', 'Parameter 1'); 
      self.box1 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ... 
            'Units', 'Normalized', 'Position', [0.4 0.6 0.2 0.1], ... 
            'String', '10'); 
      self.label2 = uicontrol('Parent', self.mainfig, 'Style', 'text', ... 
            'Units', 'Normalized', 'Position', [0.4 0.4 0.2 0.05], ... 
            'String', 'Parameter 2'); 
      self.box2 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ... 
            'Units', 'Normalized', 'Position', [0.4 0.3 0.2 0.1], ... 
            'String', '10'); 
      self.closebutton = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ... 
             'Units', 'Normalized', 'Position', [0.4 0.1 0.2 0.1], ... 
             'String', 'Close Window', 'Callback', @(h,e) self.closewindow); 
     end 

     function closewindow(self) 
      % Drop our input parameters into this window's properties 
      self.parameter1 = str2double(self.box1.String); 
      self.parameter2 = str2double(self.box2.String); 

      % Close the window 
      close(self.mainfig); 
     end 
    end 
end 
+0

안녕, 당신의 응답을 주셔서 감사합니다. 이 구성표를 사용하여 간단한 응용 프로그램을 만들려고했지만 "fprintf ('매개 변수 1 : % u \ n 매개 변수 2 : % u \ n', tmpwindow.parameter1, tmpwindow.parameter2);" MATLAB은 핸들이 삭제되었다고 말합니다. 전 UIwait 전후에 확인했는데 실제로 삭제 된 이유는 서브 윈도우에서 close 함수의 원인을 추측합니다. 문제의 근원이나 근원을 생각할 수 있습니까? 고마워요. –

+0

나는 정확한 이유로이 객체들을 객체의 속성으로 저장했습니다. 그림 창을 닫으면 더 이상 uicontrol을 지정할 수 없지만 오브젝트의 다른 등록 정보는 계속 액세스 할 수 있습니다. – excaza

관련 문제