簡體   English   中英

matlab 應用程序設計器在新應用程序中加載圖像

[英]matlab app designer load image in new app

我正在使用App Designer並嘗試將圖像(單擊后)從 MainApp 加載到 App2 並使用 MainApp 中名為imagePath的全局變量直接顯示(參見屏幕截圖),我在其中存儲字符串('metro-station.png ') 和 App2 中的Startup function ,我將 ImageSource 設置為該路徑。

但這似乎不起作用。

function startupFcn(app)
    app.Image.ImageSource = fullfile(imagePath);
end

在此處輸入圖像描述

有沒有其他方法可以做到這一點?

您不需要global variable來在兩個應用程序之間共享數據。

在應用程序之間交換數據的一種可能方式是:

在 MainApp 中

  • 定義一個public property (要與第二個 App 共享的變量)
  • 在 function 或 MainApp 的回調中初始化它
  • 在調用第二個 App 時,只需將“App”句柄作為輸入參數傳遞,這樣就可以將 MainApp 的所有公共屬性共享給第二個 App

在第二個應用程序中:

  • 定義一個private property來保存來自 MainApp 的輸入參數
  • startupFcn中將輸入參數分配給此類屬性
  • 從這一刻起,您可以訪問 ManApp 的所有public properties ,包括您希望從 MainApp 傳遞給第二個 App 的特定屬性

此方法的優點之一(但同時也是缺點之一)是您可以修改第二個 App 中的任何 MainApp 公共屬性,尤其是輸入參數

如果您不想(不需要)給第二個 App 修改 MainApp 屬性的可能性:

在主應用程序中:

  • 如果您想在調用 Second App 的函數之外的其他函數中更改 Second App 的輸入參數,您可以在 MainApp 中將其定義為private property ,以便您可以在任何 MainApp function / callback中訪問它
  • 否則,您可以在調用第二個 App 的 MainApp 的function / callback中定義一個local variable並將其用作輸入參數
  • 然后,在調用第二個應用程序時,您只能將此propertylocal變量傳遞給第二個應用程序

在第二個應用程序中:

  • 定義一個private property來存儲輸入參數
  • 在 s tartupFcn中將輸入parameter分配給該property
  • 從這一刻起,所有第二個 App function / 回調都可以訪問 ManApp 輸入參數

這種方法的優點(但同時也有缺點)是:

  • 由於輸入參數是傳值的,所以當你關閉第二個App時,對輸入參數的任何修改都會丟失
  • 第二個 App 無權訪問 MainApp 的公共屬性

其他分享方式也可以。

在以下代碼中提出的解決方案中,我使用了(為簡單起見)第一種方法。

MainApp顯示 6 個ImagesImport pushbutton ,按下該按鈕會打開second App並顯示在 MainApp 中選擇的圖像:

  • 圖像通過點擊被選中,選中時,選中(再次點擊取消選中)label 出現在其上方
  • 再次單擊圖像,取消選擇圖像
  • 單擊另一個圖像,選擇圖像並取消選擇之前選擇的圖像(如果有)
  • The pushbutton is enabled only if an image has been selected, when pressed, the second App opens.

second App只是顯示在 MainApp 中選擇的圖像, Close pushbutton允許關閉它。

MainApp 包括:(見下圖):

  • 6 圖像對象:app.Image1,...
  • 6 個標簽:app.img_1_selected,...
  • 1 個按鈕:app.ImportButton

在此處輸入圖像描述

app2 包括(見下圖):

  • 1 圖片 object:app.Image
  • 1 Label:app.Label
  • 1 個按鈕:關閉按鈕

在此處輸入圖像描述

主應用程序代碼

為簡單起見,MainApp 中圖像的路徑和文件名是硬編碼的,但您可以輕松實現 function 來動態加載它們。

public properties集中,您可以找到以下內容:

  • PathSelectedImage :用於存儲要傳遞給第二個 App 的所選圖像的路徑和文件名的屬性
  • ImgSelectionStr :存儲圖像標簽句柄的數組
  • ImageSet :一個數組,用於存儲圖像 object 的句柄

這兩個 arrays 允許在處理圖像和字符串時使用 for 循環並避免代碼重復。

arrays 在 startupFcn function 中初始化。

要訪問存儲在 arrays 中的對象的屬性,您必須使用set / get函數而不是點表示法

ImageClickedFcn callback是為每個圖像定義的; 它只是簡單地設置圖像的 ID 並調用SetUnset_Selection function。

SetUnset_Selection function管理圖像的選擇,它們上方的 label 並啟用/禁用Import pushbutton

classdef MainApp_exported < matlab.apps.AppBase

   % Properties that correspond to app components
   properties (Access = public)
      MainAppUIFigure  matlab.ui.Figure
      img_1_selected   matlab.ui.control.Label
      img_2_selected   matlab.ui.control.Label
      img_3_selected   matlab.ui.control.Label
      img_4_selected   matlab.ui.control.Label
      img_5_selected   matlab.ui.control.Label
      img_6_selected   matlab.ui.control.Label
      ImportButton     matlab.ui.control.Button
      Image_6          matlab.ui.control.Image
      Image_5          matlab.ui.control.Image
      Image_4          matlab.ui.control.Image
      Image_3          matlab.ui.control.Image
      Image_2          matlab.ui.control.Image
      Image_1          matlab.ui.control.Image
   end

   properties (Access = public)
      PathSelectedImage;   % Path and filename of the selected image
      SelectedImageId;     % Id of the selected image
      SelectedImagePrevId; % Id of the previously selected image
      ImgSelectionStr;     % Array of Label objects
      ImageSet;            % Array of Image objects
      app2_h;              % handle to access app2
   end
   
   methods (Access = private)
      
      % SetUnset_Selection: functin that manages the activaton of:
      %  - selection labels over the images 
      %  - path and filename of the selected image
      function SetUnset_Selection(app,SelectedImg)
         % Selet the image object from the ImageSet array
         tmp = app.ImageSet(SelectedImg);
         % Get the path and filename of the selected imae
         app.PathSelectedImage = get(tmp,'ImageSource');

         % Save the ID of the previouusly selected image
         if(app.SelectedImagePrevId ~= app.SelectedImageId)
            app.SelectedImagePrevId = app.SelectedImageId;
         else
            app.SelectedImagePrevId = -1;
         end
         
         % Set the ID of the selected image
         app.SelectedImageId = SelectedImg;

         % Selet the image label object from the ImgSelectionStr array
         tmp = app.ImgSelectionStr(SelectedImg);
         % Visualize the label of the selected image and hide the label of
         % the previously selected image and turns on the Import pushbutton
         if(strcmp(get(tmp,'Visible'),"off"))
            set(tmp,'Visible',"on",'Text','Selected (click again to unselect)');
            app.ImportButton.Enable = "on";
         else
            set(tmp,'Visible',"off")
            app.ImportButton.Enable = "off";
         end
         if(app.SelectedImagePrevId > 0)
            set(app.ImgSelectionStr(app.SelectedImagePrevId),'Visible','off');
         end
      end
   end
   

   % Callbacks that handle component events
   methods (Access = private)

      % Code that executes after component creation
      function startupFcn(app)
         % Initialize the handle to access app2
         app.app2_h = [];
         % Initialize the ID of the previously selected image
         app.SelectedImagePrevId = -1;
         % Initialize the ID of the selected image
         app.SelectedImageId = -1;
         % Store the image label objects in the ImgSelectionStr array
         % The array is used to easily access the label objects and avoid
         % duplication in the code
         app.ImgSelectionStr(1) = app.img_1_selected;
         app.ImgSelectionStr(2) = app.img_2_selected;
         app.ImgSelectionStr(3) = app.img_3_selected;
         app.ImgSelectionStr(4) = app.img_4_selected;
         app.ImgSelectionStr(5) = app.img_5_selected;
         app.ImgSelectionStr(6) = app.img_6_selected;

         % Store the image objects in the ImageSet array
         % The array is used to easily access the label objects and avoid
         % duplication in the code
         app.ImageSet(1) = app.Image_1;
         app.ImageSet(2) = app.Image_2;
         app.ImageSet(3) = app.Image_3;
         app.ImageSet(4) = app.Image_4;
         app.ImageSet(5) = app.Image_5;
         app.ImageSet(6) = app.Image_6;

      end

      % Button pushed function: ImportButton
      function ImportButtonPushed(app, event)
         % Turn off the Import pushbutton (will be turned on in the
         % SetUnset_Selection function
         app.ImportButton.Enable = "off";
         % Open the app2 that shows the selected image
         app.app2_h=app2(app);
      end

      % Image clicked function: Image_1
      function Image_1Clicked(app, event)
         % Set the ID of the selected image
         Sel_Img_Id = 1;
         % Call SetUnset_Selection to manage the image labels and get the
         % path and filename of the selected image
         SetUnset_Selection(app,Sel_Img_Id);

      end

      % Image clicked function: Image_2
      function Image_2Clicked(app, event)
         % Set the ID of the selected image
         Sel_Img_Id = 2;
         % Call SetUnset_Selection to manage the image labels and get the
         % path and filename of the selected image
         SetUnset_Selection(app,Sel_Img_Id);

      end

      % Image clicked function: Image_3
      function Image_3Clicked(app, event)
         % Set the ID of the selected image
         Sel_Img_Id = 3;
         % Call SetUnset_Selection to manage the image labels and get the
         % path and filename of the selected image
         SetUnset_Selection(app,Sel_Img_Id);

      end

      % Image clicked function: Image_4
      function Image_4Clicked(app, event)
         % Set the ID of the selected image
         Sel_Img_Id = 4;
         % Call SetUnset_Selection to manage the image labels and get the
         % path and filename of the selected image
         SetUnset_Selection(app,Sel_Img_Id);

      end

      % Image clicked function: Image_5
      function Image_5Clicked(app, event)
         % Set the ID of the selected image
         Sel_Img_Id = 5;
         % Call SetUnset_Selection to manage the image labels and get the
         % path and filename of the selected image
         SetUnset_Selection(app,Sel_Img_Id);

      end

      % Image clicked function: Image_6
      function Image_6Clicked(app, event)
         % Set the ID of the selected image
         Sel_Img_Id = 6;
         % Call SetUnset_Selection to manage the image labels and get the
         % path and filename of the selected image
         SetUnset_Selection(app,Sel_Img_Id);

      end
   end

   % Component initialization
   methods (Access = private)

      % Create UIFigure and components
      function createComponents(app)

         % Get the file path for locating images
         pathToMLAPP = fileparts(mfilename('fullpath'));

         % Create MainAppUIFigure and hide until all components are created
         app.MainAppUIFigure = uifigure('Visible', 'off');
         app.MainAppUIFigure.IntegerHandle = 'on';
         app.MainAppUIFigure.Position = [100 100 580 431];
         app.MainAppUIFigure.Name = 'MainApp';
         app.MainAppUIFigure.Tag = 'App1_FIGURE';

         % Create Image_1
         app.Image_1 = uiimage(app.MainAppUIFigure);
         app.Image_1.ImageClickedFcn = createCallbackFcn(app, @Image_1Clicked, true);
         app.Image_1.Position = [34 301 118 92];
         app.Image_1.ImageSource = fullfile(pathToMLAPP, 'IMG', 'img_1.jpg');

         % Create Image_2
         app.Image_2 = uiimage(app.MainAppUIFigure);
         app.Image_2.ImageClickedFcn = createCallbackFcn(app, @Image_2Clicked, true);
         app.Image_2.Position = [34 176 118 92];
         app.Image_2.ImageSource = fullfile(pathToMLAPP, 'IMG', 'img_2.jpg');

         % Create Image_3
         app.Image_3 = uiimage(app.MainAppUIFigure);
         app.Image_3.ImageClickedFcn = createCallbackFcn(app, @Image_3Clicked, true);
         app.Image_3.Position = [34 53 118 92];
         app.Image_3.ImageSource = fullfile(pathToMLAPP, 'IMG', 'img_3.jpg');

         % Create Image_4
         app.Image_4 = uiimage(app.MainAppUIFigure);
         app.Image_4.ImageClickedFcn = createCallbackFcn(app, @Image_4Clicked, true);
         app.Image_4.Position = [413 301 118 92];
         app.Image_4.ImageSource = fullfile(pathToMLAPP, 'IMG', 'img_4.jpg');

         % Create Image_5
         app.Image_5 = uiimage(app.MainAppUIFigure);
         app.Image_5.ImageClickedFcn = createCallbackFcn(app, @Image_5Clicked, true);
         app.Image_5.Position = [413 176 118 92];
         app.Image_5.ImageSource = fullfile(pathToMLAPP, 'IMG', 'img_5.jpg');

         % Create Image_6
         app.Image_6 = uiimage(app.MainAppUIFigure);
         app.Image_6.ImageClickedFcn = createCallbackFcn(app, @Image_6Clicked, true);
         app.Image_6.Position = [413 53 118 92];
         app.Image_6.ImageSource = fullfile(pathToMLAPP, 'IMG', 'img_6.jpg');

         % Create ImportButton
         app.ImportButton = uibutton(app.MainAppUIFigure, 'push');
         app.ImportButton.ButtonPushedFcn = createCallbackFcn(app, @ImportButtonPushed, true);
         app.ImportButton.BackgroundColor = [0.9294 0.6941 0.1255];
         app.ImportButton.FontSize = 14;
         app.ImportButton.FontWeight = 'bold';
         app.ImportButton.Enable = 'off';
         app.ImportButton.Position = [211 203 158 39];
         app.ImportButton.Text = 'Import';

         % Create img_6_selected
         app.img_6_selected = uilabel(app.MainAppUIFigure);
         app.img_6_selected.FontWeight = 'bold';
         app.img_6_selected.FontColor = [1 0 0];
         app.img_6_selected.Visible = 'off';
         app.img_6_selected.Position = [350 144 195 22];
         app.img_6_selected.Text = 'Selected (click again to unselect)';

         % Create img_5_selected
         app.img_5_selected = uilabel(app.MainAppUIFigure);
         app.img_5_selected.FontWeight = 'bold';
         app.img_5_selected.FontColor = [1 0 0];
         app.img_5_selected.Visible = 'off';
         app.img_5_selected.Position = [350 267 195 22];
         app.img_5_selected.Text = 'Selected (click again to unselect)';

         % Create img_4_selected
         app.img_4_selected = uilabel(app.MainAppUIFigure);
         app.img_4_selected.FontWeight = 'bold';
         app.img_4_selected.FontColor = [1 0 0];
         app.img_4_selected.Visible = 'off';
         app.img_4_selected.Position = [350 393 195 22];
         app.img_4_selected.Text = 'Selected (click again to unselect)';

         % Create img_3_selected
         app.img_3_selected = uilabel(app.MainAppUIFigure);
         app.img_3_selected.FontWeight = 'bold';
         app.img_3_selected.FontColor = [1 0 0];
         app.img_3_selected.Visible = 'off';
         app.img_3_selected.Position = [48 144 195 22];
         app.img_3_selected.Text = 'Selected (click again to unselect)';

         % Create img_2_selected
         app.img_2_selected = uilabel(app.MainAppUIFigure);
         app.img_2_selected.FontWeight = 'bold';
         app.img_2_selected.FontColor = [1 0 0];
         app.img_2_selected.Visible = 'off';
         app.img_2_selected.Position = [48 267 195 22];
         app.img_2_selected.Text = 'Selected (click again to unselect)';

         % Create img_1_selected
         app.img_1_selected = uilabel(app.MainAppUIFigure);
         app.img_1_selected.FontWeight = 'bold';
         app.img_1_selected.FontColor = [1 0 0];
         app.img_1_selected.Visible = 'off';
         app.img_1_selected.Position = [48 393 195 22];
         app.img_1_selected.Text = 'Selected (click again to unselect)';

         % Show the figure after all components are created
         app.MainAppUIFigure.Visible = 'on';
      end
   end

   % App creation and deletion
   methods (Access = public)

      % Construct app
      function app = MainApp_exported

         % Create UIFigure and components
         createComponents(app)

         % Register the app with App Designer
         registerApp(app, app.MainAppUIFigure)

         % Execute the startup function
         runStartupFcn(app, @startupFcn)

         if nargout == 0
            clear app
         end
      end

      % Code that executes before app deletion
      function delete(app)

         % Delete UIFigure when app is deleted
         delete(app.MainAppUIFigure)
      end
   end
end

第二個應用程序代碼

private property MainApp用於存放MainApp的入參(即MainApp句柄)

startupFcn只是從輸入參數中檢索要顯示的圖像的路徑和名稱,並在圖像上方設置 label。

classdef app2_exported < matlab.apps.AppBase

   % Properties that correspond to app components
   properties (Access = public)
      App2ImageDisplayUIFigure  matlab.ui.Figure
      Label                     matlab.ui.control.Label
      CloseButton               matlab.ui.control.Button
      Image                     matlab.ui.control.Image
   end

   
   properties (Access = private)
      MainApp; % Handle of the calling app (app1)
   end
   

   % Callbacks that handle component events
   methods (Access = private)

      % Code that executes after component creation
      function startupFcn(app, CallingApp)
         % Set the handle of the calling app
         app.MainApp = CallingApp;
         % Load the select image
         app.Image.ImageSource = CallingApp.PathSelectedImage;
         % Print the name and the ID of the diplayed image
         [~,name,~] = fileparts(app.Image.ImageSource);
         img_id=CallingApp.SelectedImageId;
         str=sprintf('Name: %s\nPosition: %d',name,img_id);
         app.Label.Text = str;
         % Set the Label of the selected image in app1 as "Imported"
         tmp=CallingApp.ImgSelectionStr(img_id);
         set(tmp,'text','Imported');

      end

      % Button pushed function: CloseButton
      function CloseButtonPushed(app, event)
         % Get the handle of the label of the selected image in app1
         tmp=app.MainApp.ImgSelectionStr(app.MainApp.SelectedImageId);
         % Update the label of displayed image in the calling app before
         % closing the app2
         set(tmp,'text','App2 closed, select a in image');
         app.MainApp.app2_h = [];
         delete(app);
      end

      % Close request function: App2ImageDisplayUIFigure
      function App2ImageDisplayUIFigureCloseRequest(app, event)
         tmp=app.MainApp.ImgSelectionStr(app.MainApp.SelectedImageId);
         % Update the label of displayed image in the calling app before
         % closing the app2
         set(tmp,'text','App2 closed, select a in image');
         app.MainApp.app2_h = [];
         delete(app);
         
      end
   end

   % Component initialization
   methods (Access = private)

      % Create UIFigure and components
      function createComponents(app)

         % Create App2ImageDisplayUIFigure and hide until all components are created
         app.App2ImageDisplayUIFigure = uifigure('Visible', 'off');
         app.App2ImageDisplayUIFigure.IntegerHandle = 'on';
         app.App2ImageDisplayUIFigure.Position = [100 100 655 536];
         app.App2ImageDisplayUIFigure.Name = 'App2 - Image Display';
         app.App2ImageDisplayUIFigure.CloseRequestFcn = createCallbackFcn(app, @App2ImageDisplayUIFigureCloseRequest, true);
         app.App2ImageDisplayUIFigure.HandleVisibility = 'on';
         app.App2ImageDisplayUIFigure.Tag = 'app2_FIGURE';

         % Create Image
         app.Image = uiimage(app.App2ImageDisplayUIFigure);
         app.Image.Position = [33 122 390 295];

         % Create CloseButton
         app.CloseButton = uibutton(app.App2ImageDisplayUIFigure, 'push');
         app.CloseButton.ButtonPushedFcn = createCallbackFcn(app, @CloseButtonPushed, true);
         app.CloseButton.Position = [445 273 98 34];
         app.CloseButton.Text = 'Close';

         % Create Label
         app.Label = uilabel(app.App2ImageDisplayUIFigure);
         app.Label.WordWrap = 'on';
         app.Label.Position = [59 448 524 73];
         app.Label.Text = '';

         % Show the figure after all components are created
         app.App2ImageDisplayUIFigure.Visible = 'on';
      end
   end

   % App creation and deletion
   methods (Access = public)

      % Construct app
      function app = app2_exported(varargin)

         % Create UIFigure and components
         createComponents(app)

         % Register the app with App Designer
         registerApp(app, app.App2ImageDisplayUIFigure)

         % Execute the startup function
         runStartupFcn(app, @(app)startupFcn(app, varargin{:}))

         if nargout == 0
            clear app
         end
      end

      % Code that executes before app deletion
      function delete(app)

         % Delete UIFigure when app is deleted
         delete(app.App2ImageDisplayUIFigure)
      end
   end
end

下圖顯示了兩個“正在工作”的應用程序。

在此處輸入圖像描述

希望這可以幫助。 卡普拉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM