前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MATLAB GUI表格(uitable)的增删操作

MATLAB GUI表格(uitable)的增删操作

作者头像
全栈程序员站长
发布2022-09-07 17:14:01
2.5K0
发布2022-09-07 17:14:01
举报

大家好,又见面了,我是你们的朋友全栈君。

这几天,查看了很多的MATLAB GUI 表格的操作,发现都没有一个完整的增删改的帖子。于是在我自己摸索下,自己搞出来了,增删操作。接下来就分享给大家!

界面布局:

表格的tag: uitable1

添加电价的tag:addEle

删除电价的tag:delEle

MATLAB GUI表格(uitable)的增删操作
MATLAB GUI表格(uitable)的增删操作

首先建立一个 newData.mat,用于存放表格数据:

MATLAB GUI表格(uitable)的增删操作
MATLAB GUI表格(uitable)的增删操作

在打开窗体的时候,加载 newData.mat 文件,并且显示:

MATLAB GUI表格(uitable)的增删操作
MATLAB GUI表格(uitable)的增删操作

添加数据,我是通过 对话框来实现的:

MATLAB GUI表格(uitable)的增删操作
MATLAB GUI表格(uitable)的增删操作

代码:

MATLAB GUI表格(uitable)的增删操作
MATLAB GUI表格(uitable)的增删操作

增加功能就完成了。接下来是删除功能:

1.删除功能,需要用到 表格的一个回调函数 CellSelectionCallback:

MATLAB GUI表格(uitable)的增删操作
MATLAB GUI表格(uitable)的增删操作

2.删除功能;

MATLAB GUI表格(uitable)的增删操作
MATLAB GUI表格(uitable)的增删操作

全部代码:

代码语言:javascript
复制
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);  %更新结构体

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/136639.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年6月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 这几天,查看了很多的MATLAB GUI 表格的操作,发现都没有一个完整的增删改的帖子。于是在我自己摸索下,自己搞出来了,增删操作。接下来就分享给大家!
  • 界面布局:
    • 首先建立一个 newData.mat,用于存放表格数据:
      • 在打开窗体的时候,加载 newData.mat 文件,并且显示:
        • 添加数据,我是通过 对话框来实现的:
          • 代码:
            • 增加功能就完成了。接下来是删除功能:
              • 1.删除功能,需要用到 表格的一个回调函数 CellSelectionCallback:
                • 2.删除功能;
                • 全部代码:
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档