MATLAB GUI ,2,使用MATLAB的函數來實現(xiàn)MATLAB GUI,part 1,圖
Jun 07, 2016 pm 03:19 PM一 概述 其實MATLAB的GUI完全可以使用MATLAB中各種控件的函數來實現(xiàn),也可以說GUI就是一個figure,本人也是對MATLAB GUI比較感興趣的一個學習者,在學習MATLAB過程中,搜索了不少資料才了解了用函數形式也就是不依靠GUIDE完成GUI設計的方法,在這里總結分享
一? 概述
??? 其實MATLAB的GUI完全可以使用MATLAB中各種控件的函數來實現(xiàn),也可以說GUI就是一個figure,本人也是對MATLAB GUI比較感興趣的一個學習者,在學習MATLAB過程中,搜索了不少資料才了解了用函數形式也就是不依靠GUIDE完成GUI設計的方法,在這里總結分享,若有錯誤,還望網友指出。
??? MATLAB GUI,2這一部分的內容以函數的形式實現(xiàn)一個簡單的跳繩的小游戲,包括一個簡單的開始界面,和死亡界面,非常簡單,而且不涉及任何有難度的算法,僅僅是作為對MATLAB GUI的函數實現(xiàn)這一形式的參考,而這一篇為第一部分,主要介紹將要用到的一些控件。
?
二? 圖像(figure)
??? 圖像就不多說了,主要介紹下圖像的句柄。
??? figure_handler = figure;???????? %多嘴一句,盡量在有返回值的語句后面加分號,會提高運行速度
??? 上面一行代碼是建立一個默認圖像,并將圖像的句柄返回至handler中,那么我們想對建立figure進行任何操作,只需要改變handler的屬性值即可。
??? 在建立figure的時候,就可以改變figure的屬性值,例如,
??? figure_handler = figure('name', 'test', 'numbertitle', 'off');
??? 通過上面語句建立的圖像,關閉了數字標題,并把圖像名設置為test,那么新建的圖像在標題欄就只會顯示test了。
??? 本文所需figure屬性如下
??? WindowKeyPressFcn :圖像窗口鍵盤按下回調函數,當圖像窗口被設置為當前窗口時,按下鍵盤上的鍵時調用該函數
????name :圖像名,也是窗口名,值為字符串
??? units :圖像中位置等信息的衡量單位,常用值為'normalized'(歸一化值)以及'pixel'(像素)
??? position :按鈕在圖像中的位置,4維向量,[左起始點,下起始點,寬度,高度]
??? numbertitle :顯示在窗口欄的數字標題,這里沒用,設置為'off'或'on'
??? resize :標識窗口是否能被用戶重新定義大小,設置為'off'或'on'
??? menubar :圖像的菜單欄,可以是菜單欄句柄,也可以為'none'或'fiure','none'表示無菜單欄,'figure'為默認繪圖窗口的菜單欄
??? toolbar :圖像的工具欄,可以是工具欄句柄,也可以為'none'或'fiure','none'表示無工具欄,'figure'為默認繪圖窗口的工具欄
?
?
三? set和get函數
??? 如果要使用好MATLAB GUI,那么set和get函數是最基本的。set函數的作用是設置句柄的屬性值,原型如下
??? set(figure_handler, 'property1', value1, 'property2', value2)
??? 而get函數的作用是獲取句柄的屬性值,原型如下
??? value = ge(figure_handler, 'property');
??? 那么,如果想要改變前面建立的圖像的鼠標,那么只需要執(zhí)行如下語句即可
??? set(figure_handler, 'pointer', 'cross');
??? 現(xiàn)在,把鼠標移動到剛剛建立的圖像上,觀察下鼠標,你會發(fā)現(xiàn)不同的。如果想知道更多種類,在幫助文檔中查找 figure properties。
??? 關于set和get以及figure的屬性,更多可以參考下 http://blog.csdn.net/szv123_rier/article/details/8157218
?
四? 在圖像上建立按鈕
??? 我們要使用的是按鈕pushbutton,通過按鈕來完成一些交互。按鈕通過函數uicontrol來建立,調用方式如下。
??? button_handler = uicontrol(figure_handler, 'style', 'pushbutton');
??? fiugre_handler作為按鈕的父句柄,那么這個按鈕的句柄是圖像的子句柄。'style'定義風格,這里是'pushbutton'。建立時可以同時定義其他屬性,本文用到的按鈕的屬性值如下,
??? units :圖像中位置等信息的衡量單位,常用值為'normalized'(歸一化值)以及'pixel'(像素)
??? position :按鈕在圖像中的位置,4維向量,[左起始點,下起始點,寬度,高度]
??? string :顯示在按鈕上的字符串
??? fontsize :有字符當然有字體大小了
??? callback :回調函數,當按鈕被按下時執(zhí)行的函數
?
五? 坐標軸(axes)
??? 坐標軸是顯示圖像的控件,MATLAB的plot函數在axes上完成,也就是說整個小游戲都是繪制在這里的。
??? 建立方式如下,
??? axes_handler = axes;
??? 同樣,也可以在建立時,改變其他屬性。本文需要的屬性值如下,
??? units, position :同上
??? ylim :2維向量,[Y軸最小值,Y軸最大值],標記坐標軸中Y軸顯示范圍。
??? xlim :同ylim,只是這是關于X軸的。
??? tickdir :tick是坐標軸中的標記指針,就是在軸上的刻度線,tickdir設置為刻度線的方向,'out',’in' 可選,表示指向坐標軸內或外
?
六? 小結
??? 簡單介紹了下圖像、按鈕和坐標軸的建立以及一些屬性,以及操作屬性的set和get函數。結束前完成一個簡單的例子作為鞏固吧。
?
??? fh = figure('name','part1demo',???? ...? 設置圖像名
?????????? ?'unit','pixel',???????????????????????? ? ...? 設置單位,與按鈕一樣,這里設置為像素,好調整位置
??????????? 'position',[240,70,800,600],? ...? 假設電腦是1280X768的分辨率,那么我設置一個800X600的圖像,盡量在屏幕中央
??????? ??? 'numbertitle','off',??????????????????...? 關閉figure的數字標題
??????? ??? 'resize','off',????????????????????????? ...? 不支持窗口大小的變換
??????? ??? 'menubar','none',???????????????? ...? 取消默認菜單欄
??????? ??? 'toolbar','none'????????????????????? ...? 取消默認工具欄
??? );?
??? ah = axes('units','normalized',???? ...? 設置單位,設置為歸一化,
?????? ??? 'position',[0,0,1,1],???????? ?????? ...? 鋪滿整個窗口
?????? ??? 'tickdir','out',????????????????????? ?? ...? 設置坐標軸標記的方向為向外,這樣在坐標軸上就不會看到了
?????? ??? 'xlim',[1,800],?????????????????????? ...? 設置x軸范圍為1到800
?????? ??? 'ylim',[1,600]????????????????????? ? ...? 設置y軸范圍為1到600
??? );
??? bh = uicontrol(fh,???????????????????? ?...? 父句柄為剛剛建立的圖像句柄
????????? 'style','pushbutton',???????? ??? ...? 風格為按鈕
???????? ?'string','繪制一個正弦波 - -',?? ...? 設置按鈕文字
????????? 'fontsize',40,??????????????????? ...? 設置字體大小
????????? 'units','normalized',?????????? ...? 設置單位為歸一化
????? ??? 'position',[0,0,1,0.2],???????? ...? 設置按鈕在窗口下方
????? ??? 'callback','plot(sin(1:800))'?? ...? 設置按鈕的回調函數,在坐標軸中繪制正弦波
??? );
?
???? oops,例子里忘了添加set和get了,不過沒事,建立完成后若想改變其屬性,使用set就好了,就不多寫了。
來張截圖,點擊按鈕前
點擊按鈕后
???
???

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

By encapsulating code, C++ functions can improve GUI development efficiency: Code encapsulation: Functions group code into independent units, making the code easier to understand and maintain. Reusability: Functions create common functionality that can be reused across applications, reducing duplication and errors. Concise code: Encapsulated code makes the main logic concise and easy to read and debug.
