大家好,又见面了,我是你们的朋友全栈君。
这几天,查看了很多的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)
% 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); %更新结构体
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136639.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...