軟件環境
軟件版本是matlab2019b,App Designer是matlab2016a以后的版本中才有的模塊。
組件的使用
主窗口UIFigure
Position:(x,y,width,height),相對於父容器左下角的位置。
窗口加載時居中:
進入主窗口啟動函數回調
function startupFcn(app) screen=get(0,"ScreenSize");%獲取屏幕尺寸,返回(1,1,width,height) screenX=(screen(3)-1200)/2;%窗體左下角X坐標 screenY=(screen(4)-720)/2;%窗體左下角Y坐標 app.UIFigure.Position=[screenX,screenY,1200,720];%窗體位置,寬1200px,高720px %還可以進行其他界面組件屬性的初始化 end
關閉主窗口時增加確認提示
function UIFigureCloseRequest(app, event) result=questdlg("確定退出?","提示","是","否","是"); if result=='是' delete(app); end end
面板Panel
多個小的組件可以放在一個面板中,方便移動。
面板組TabGroup
借助按鈕等控件實現面板跳轉 ,例如想通過一個按鈕從當前的所在的Tab跳轉到Tab2中
function ButtonValueChanged(app, event) app.TabGroup.SelectedTab=app.Tab2; end
文本框TextArea、數值/文本框EditField
需要輸入數值時優先使用數值框。
文本框中,改變標簽上的字符串:app.TextAreaLabel.Text、app.EditFieldLabel.Text
改變值:app.TextAreaLabel.Value、app.EditFieldLabel.Value
標簽Label
app.Label.Text
按鈕Button
可以為按鈕增加圖標
需要注意的是,需要加入圖片文件所在路徑
這里推薦一個阿里的圖標素材網站:
下拉框DropDown
app.DropDown.Value值為當前選項的值,為字符串,如果是數值型的值,可以使用eval()或str2double()將字符串類型轉換為數值。
app.DropDown.Items和app.DropDown.ItemsData相對應,一般不需要改變。
單選按鈕組
app.ButtonGroup.SelectedObject.Value取到所選按鈕的值,0(false)或1(true)
復選框
app.CheckBox2.Value取到所選按鈕的值,0(false)或1(true)
列表框ListBox
列表框的單選、多選
Multiselect
可以不使用其ItemsData屬性,只使用Items屬性
實現Items屬性插入、排序、刪除
新增元素
app.ListBox.Items=[app.ListBox.Items,num2str(num)];
排序:
app.ListBox.Items=string(sort(str2double(app.ListBox.Items),"descend"));%降序
app.ListBox.Items=string(sort(str2double(app.ListBox.Items),"ascend"));%升序
刪除
如果有ItemsData屬性
value=app.ListBox.Value; for n=length(app.ListBox.ItemsData):-1:1 if value==app.ListBox.ItemsData{n} app.ListBox.ItemsData(n)=[]; app.ListBox.Items=string(app.ListBox.ItemsData); break; end end
如果只用了Items,需要做數據類型的轉換,邏輯相同。