MATLAB之GUI学习经典举例
生活随笔
收集整理的這篇文章主要介紹了
MATLAB之GUI学习经典举例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1. 設(shè)置背景顏色
- 1.1 通過GUI按鈕設(shè)置背景顏色
- 1.2 通過GUI運(yùn)行設(shè)置背景顏色
- 2. 錯(cuò)誤對(duì)話框
- 2.1 “截屏失敗”舉例
- 3. 計(jì)算器
- 按鈕callback
- +-*/ callback
- = callback
- √ callback
- ← callback
- C callback
- 4. 退出
- 5. 選擇列表框繪圖
- 6. 取彈出式菜單里的值
1. 設(shè)置背景顏色
1.1 通過GUI按鈕設(shè)置背景顏色
function backswitch_Callback(hObject, eventdata, handles) % hObject handle to backswitch (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) ha = axes('units','normalized','position',[0 0 1 1]); uistack(ha, 'down') [fname, pname, index] = uigetfile({'*.jpg; *.bmp'},'選擇背景圖片'); if indexII = imread([pname,fname]);image(II)colormap grayset(ha,'handlevisibility','off','visible','off'); elseerrordlg('選擇背景失敗'); %提示錯(cuò)誤對(duì)話框hDialog = findall(0,'tag','Msgbox_Error Dialog');btn_ok = findall(hDialog,'style','pushbutton');set(btn_ok,'String','確定'); end1.2 通過GUI運(yùn)行設(shè)置背景顏色
% --- Executes just before computesumprice is made visible. function computesumprice_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to computesumprice (see VARARGIN)% Choose default command line output for computesumprice ha = axes('units','normalized','position',[0 0 1 1]); uistack(ha, 'down') % up,down,可選 II = imread('1.jpg'); image(II) colormap gray set(ha,'handlevisibility','off','visible','off');handles.output = hObject; % Update handles structure guidata(hObject, handles);2. 錯(cuò)誤對(duì)話框
2.1 “截屏失敗”舉例
% --- Executes on button press in screencapture. function screencapture_Callback(hObject, eventdata, handles) % hObject handle to screencapture (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [fname, ~, index] = uiputfile({'*.jpg'}); if indexa = getframe(computesumprice);%注意不是gcfimwrite(a.cdata,fname); elseerrordlg('導(dǎo)出失敗'); %提示錯(cuò)誤對(duì)話框hDialog = findall(0,'tag','Msgbox_Error Dialog');btn_ok = findall(hDialog,'style','pushbutton');set(btn_ok,'String','確定'); end3. 計(jì)算器
按鈕callback
str_cur = get(handles.display, 'string'); str_cur_val = str2double(get(handles.display, 'string')); current(1) = get(hObject, 'string'); % 判斷初始化是否為0 if str_cur_val == 0set(handles.display,'string',current(1)); elseset(handles.display,'string',[str_cur,current(1)]); end±*/ callback
str_before = get(handles.display, 'string'); add_str = get(handles.add, 'string'); set(handles.display,'string',[str_before,add_str]);= callback
equal_sign = get(hObject,'string');%獲取= str_before = get(handles.display, 'string'); %獲取display的字符 answer = eval(str_before);%MATLAB提供的eval函數(shù),可根據(jù)字符直接計(jì)算,無需格外編寫算法 set(handles.display,'string',strcat(str_before,equal_sign,num2str(answer)));√ callback
str_before = get(handles.display, 'string'); square_root_str = 'sqrt'; leftk ='('; rightk =')'; set(handles.display,'string',[square_root_str,leftk ,str_before,rightk]);← callback
textString = get(handles.display,'String'); n=length(textString); t=char(textString); textString=t(1:n-1); set(handles.display,'String',textString)C callback
set(handles.display , 'string', num2str(0));4. 退出
h = gcf; close(h);5. 選擇列表框繪圖
seldata = get(handles.seldata, 'value'); %seldata為列表框tag axes(handles.axes2)switch seldatacase 1plot(t,data1)title('傳聲器1時(shí)域數(shù)據(jù)')case 2plot(t,data2)title('傳聲器2時(shí)域數(shù)據(jù)') otherwisereturn end6. 取彈出式菜單里的值
winType = get(handles.winType, 'value'); switch winTypecase 1w = hanning(NFFT);case 2w = boxcar(NFFT); case 3w = hamming(NFFT);otherwisereturn end總結(jié)
以上是生活随笔為你收集整理的MATLAB之GUI学习经典举例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB随机信号统计特征
- 下一篇: MATLAB之相关函数