首页 > 学院 > 开发设计 > 正文

matlab 动态增加uitable内容

2019-11-08 18:21:52
字体:
来源:转载
供稿:网友

matlab 动态增加uitable内容

在利用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控件时,初始化其内的数据为空。


上一篇:第13章 使用sed处理文本

下一篇:PAT 1046

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表