guimatlab数组表格_MATLABGUI表格(uitable)的增删操作这⼏天,查看了很多的MATLAB GUI 表格的操作,发现都没有⼀个完整的增删改的帖⼦。于是在我⾃⼰摸索下,⾃⼰搞出来了,增删操作。接下来就分享给⼤家!
界⾯布局:
表格的tag: uitable1
添加电价的tag:addEle
删除电价的tag:delEle
⾸先建⽴⼀个 newData.mat,⽤于存放表格数据:
在打开窗体的时候,加载 newData.mat ⽂件,并且显⽰:
添加数据,我是通过 对话框来实现的:
代码:
增加功能就完成了。接下来是删除功能:
1.删除功能,需要⽤到 表格的⼀个回调函数 CellSelectionCallback:
2.删除功能;
全部代码:
function varargout = demo(varargin)
% DEMO MATLAB code for demo.fig
% DEMO, by itself, creates a new DEMO or raises the existing
% singleton*.
%
% H = DEMO returns the handle to a new DEMO or the handle to
% the existing singleton*.
%
% DEMO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DEMO.M with the given input arguments.
%
% DEMO('Property','Value',...) creates a new DEMO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before demo_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to demo_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help demo
% Last Modified by GUIDE v2.5 23-Sep-2018 10:31:19
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @demo_OpeningFcn, ...
'gui_OutputFcn', @demo_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before demo is made visible.
function demo_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 demo (see VARARGIN)
% Choose default command line output for demo
%********代码编写***********
load('newData.mat');
set(handles.uitable1,'Data',newData);
%**************************************************
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes demo wait for user response (see UIRESUME)
matlab数组赋值% uiwait(handles.figure1);
function uitable1_CreateFcn(hObject, eventdata, handles, varargin) handles.output = hObject;
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line. function varargout = demo_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = handles.output;
% --- Executes on button press in addEle.
function addEle_Callback(hObject, eventdata, handles)
% hObject handle to addEle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%***********代码编写****************
%点击 增加后弹出 对话框
prompt ={'时间段','买电(元/KWh)','卖电(元/KWh)'}; %对话框内容提⽰
title = '请输⼊数据'; %对话框标题
lines = [1,1,1]; %设置输⼊框⾏数
def = { '正常','1.2','0.5'}; %默认值
tab = inputdlg(prompt,title,lines,def); %对话框设置
newrow1 = tab{1}; %对话框第⼀⾏内容
newrow2 = str2num(tab{2}); %对话框第⼆⾏内容
newrow3 = str2num(tab{3}); %对话框第三⾏内容
newArray = {newrow1, newrow2, newrow3}; %保存在新的矩阵中
oldData = get(handles.uitable1,'Data') %保存原来的数据
newData = [oldData;newArray]; %新的数据源
set(handles.uitable1,'Data',newData); %显⽰到表格中
%handles.tabale = newData;
save('newData.mat','newData'); %把数据永久性保存,⽅便下次使⽤
% --- Executes on button press in delEle.
function delEle_Callback(hObject, eventdata, handles)
% hObject handle to delEle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%arr=get(handles.uitable1,'Data')
hangIndex = handles.hangIndex; %获取选择以后传⼊的 ⾏索引
newData = get(handles.uitable1,'Data'); %获取表格数据矩阵
newData(hangIndex,:) = []; %删除选中的某⾏数据
set(handles.uitable1,'Data',newData); %显⽰到表格中
save('newData.mat','newData'); %删除以后,保存⼀次数据
% --- Executes when selected cell(s) is changed in uitable1.
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE) %Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
newData = get(hObject,'Data'); %获取数据矩阵
hang = eventdata.Indices; %获取⾏索引
hangIndex = hang(1); %⾏索引赋值
handles.hangIndex = hangIndex; %把⾏索引添加到结构体
guidata(hObject, handles); %更新结构体

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。