MATLAB App 構建中的複選框


MATLAB 提供了一個整合的 App 構建工具箱,我們可以使用它來建立基於圖形使用者介面的應用程式,而無需編寫任何程式碼。因此,MATLAB 允許使用者只需透過拖放功能即可建立專業的應用程式。之後,使用者可以編寫 MATLAB 程式碼來定義應用程式選項的行為。

如上所述,本文主要用於在應用程式中建立使用者介面,該介面提供一系列選項,使用者可以從中選擇任意數量的選擇。此使用者介面稱為複選框。

複選框通常顯示為一個小方塊,允許使用者透過選中或取消選中該框來選擇或取消選擇選項。

在 MATLAB App 中建立複選框的步驟

下面解釋了在 MATLAB 應用程式中建立複選框的分步過程

步驟 1 - 開啟 MATLAB 並選擇“APPS”選項卡,然後選擇功能區上的“設計 App”選項。

步驟 2 - 透過單擊“新建”選項建立一個空白 App。將開啟一個新視窗,其中包含許多元件,如下面的圖所示。

這裡,元件庫顯示在左側欄中,元件屬性窗格顯示在右側欄中。

步驟 3 - 將“複選框”選項從元件庫拖放到視窗中間顯示的畫布上。

當我們這樣做時,一個複選框將出現在畫布上,並且一個用於自定義複選框屬性的窗格將出現在右側欄中。

步驟 4 - 定義複選框的各種屬性

  • 複選框 - 使用此屬性,我們可以更改複選框的值和文字標籤。

  • 字型 - 使用此屬性,我們可以更改複選框的字型、文字大小、文字顏色和文字角度。

  • 互動性 - 此屬性用於使複選框可見、啟用、顯示工具提示或建立上下文選單。

  • 位置 - 此屬性允許我們更改複選框在畫布上的位置和尺寸。

  • 回撥執行控制 - 此屬性用於控制複選框的中斷能力和繁忙操作。

  • 父子關係 - 此屬性控制複選框的控制代碼可見性。

  • 識別符號 - 此屬性允許我們向複選框新增標籤。

步驟 5 - 最後編寫 MATLAB 程式碼來定義複選框的功能。

MATLAB 中的複選框建立示例

步驟 1 - 建立五個帶有標籤“教程”、“電子書”、“影片講座”、“文章”和“證書課程”的複選框。

步驟 2 - 現在,編寫 MATLAB 程式碼為每個複選框新增功能。為此,右鍵單擊第一個複選框“教程”,然後單擊“回撥”選項,然後選擇“新增 ValueChangedFcn 回撥”選項。

單擊“新增 ValueChangedFcn 回撥”選項後,它將帶我們進入程式碼視窗,在該視窗中它新增一個名為“TutorialsCheckBoxValueChanged()”的函式。

步驟 3 - 我們將在提供的空間中編寫程式碼來新增功能。在這種情況下,我們正在定義以下功能

if value == 1
   fprintf(‘You have selected Tutorials);
end

我們將為每個複選框編寫此程式碼。

示例

% MATLAB program to demonstrate adding checkboxes in an app
classdef app1 < matlab.apps.AppBase
   % Properties that correspond to app components
   properties (Access = public)
     UIFigure                    matlab.ui.Figure
     CertificateCoursesCheckBox  matlab.ui.control.CheckBox
     ArticlesCheckBox            matlab.ui.control.CheckBox
     VideoLecturesCheckBox       matlab.ui.control.CheckBox
     EBooksCheckBox              matlab.ui.control.CheckBox
     TutorialsCheckBox           matlab.ui.control.CheckBox
   end

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

      % Value changed function: TutorialsCheckBox
      function TutorialsCheckBoxValueChanged(app, event)
         value = app.TutorialsCheckBox.Value;

         if value == 1
            fprintf('You have selected Tutorials');
         end
      end

      % Value changed function: EBooksCheckBox
      function EBooksCheckBoxValueChanged(app, event)
         value = app.EBooksCheckBox.Value;
            
         if value == 1
            fprintf('You have selected Ebooks');
         end
      end

      % Value changed function: VideoLecturesCheckBox
      function VideoLecturesCheckBoxValueChanged(app, event)
         value = app.VideoLecturesCheckBox.Value;
            
         if value == 1
             fprintf('You have selected Video Lectures');
            end
        end

      % Value changed function: ArticlesCheckBox
      function ArticlesCheckBoxValueChanged(app, event)
         value = app.ArticlesCheckBox.Value;
            
         if value == 1
            fprintf('You have selected Articles');
         end
     end

     % Value changed function: CertificateCoursesCheckBox
     function CertificateCoursesCheckBoxValueChanged(app, event)
         value = app.CertificateCoursesCheckBox.Value;
            
         if value == 1
           fprintf('You have selected Certificate Courses');
         end
      end
   end

   % Component initialization
   methods (Access = private)

      % Create UIFigure and components
      function createComponents(app)

         % Create UIFigure and hide until all components are created
         app.UIFigure = uifigure('Visible', 'off');
         app.UIFigure.Position = [100 100 640 480];
         app.UIFigure.Name = 'MATLAB App';

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

   % App creation and deletion
   methods (Access = public)

      % Construct app
      function app = app1

         % Create UIFigure and components
         createComponents(app)

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

         if nargout == 0
            clear app
         end
      end

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

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

輸出

You have selected Tutorials
You have selected Video Lectures

透過這種方式,我們可以在 MATLAB 應用程式中建立複選框。

更新於:2023年7月18日

瀏覽量:187

啟動您的 職業生涯

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.