在利用matlab設計GUI程序時,uitable控件通常用來展示數據。而這些需要展示在GUI面板中的數據不是一層不變的,有的時候會動態地增加和變動。當數據需要動態增加時,需要考慮橫向和縱向數據增加的情況。因此總結以下uitable數據橫向和縱向動態增加的情況。
1、橫向增加
設計一個GUI程序如下圖所示:

主要的功能是橫向地增加數據并在uitable內顯示數據。主要效果如下所示:
-----------------
代碼如下:
% --- Executes on button PRess in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%增加一列的按鈕函數。對應的按鈕為圖中的【增加】按鈕的功能content1 = str2double(get(handles.edit1,'String'));content2 = str2double(get(handles.edit2,'String'));content3 = str2double(get(handles.edit3,'String'));content4 = str2double(get(handles.edit4,'String'));uitabledata = get(handles.uitable1,'data');handles.uitabledata = uitabledata;%將uitable內原來的數據保留下來,方便下次撤下動作時的數據恢復guidata(hObject,handles);%將content1-4和原來的數據組織成為uitable控件數據olddata = uitabledata;newcol = [content1 content2 content3 content4]';if isempty(olddata) newdata = newcol;else newdata = [olddata newcol];endset(handles.uitable1,'data',newdata);%將新增加的數據和原來的數據組成newdata,然后寫入uitable1中。% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%將剛才添加的那一列撤銷,對應于圖中的【重置】按鈕的功能olddata = handles.uitabledata;set(handles.uitable1,'data',olddata);% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%將整個uitable清空,對應于圖中的【清空】按鈕的功能set(handles.uitable1,'data',[]);% --- Executes during object creation, after setting all properties.function uitable1_CreateFcn(hObject, eventdata, handles)% hObject handle to uitable1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns calledset(hObject,'data',[]);%進入該GUI界面時,初始化uitable內容為空。2、縱向增加
縱向增加數據并動態地在uitable控件中顯示,即增加uitable內數據的行數,基本原理類似。
首先設計如下GUI界面:
其效果如下所示:
.................................
具體程序如下:
% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%縱向動態增加數據,對應圖中的【增加】按鈕功能%首先將edit控件內的數據取出來content1 = str2double(get(handles.edit1,'string'));content2 = str2double(get(handles.edit2,'string'));content3 = str2double(get(handles.edit3,'string'));%取出原來uitable內容,并保存到handles內便于數據的重置操作uitabledata = get(handles.uitable1,'data');handles.uitabledata = uitabledata;guidata(hObject,handles);%將原來uitable的內容和新的內容組織成新數據olddata = uitabledata;newrow = [content1 content2 content3];newdata = [olddata;newrow]; %將新數據寫入uitable內set(handles.uitable1,'data',newdata);% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%撤銷剛才增加的那行數據,對應圖中的【重置】按鈕功能set(handles.uitable1,'data',handles.uitabledata);% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%清空該uitable控件內容,對應圖中的【清空】按鈕功能set(handles.uitable1,'data',[]);% --- Executes during object creation, after setting all properties.function uitable1_CreateFcn(hObject, eventdata, handles)% hObject handle to uitable1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns calledset(hObject,'data',[]);%進入GUI頁面程序并顯示該uitable控件時,初始化其內的數據為空。
新聞熱點
疑難解答