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

MATLAB GUI表格(uitable)的增删操作这几天,查看了很多的MATLABGUI表格的操作,发现都没有一个完整的增删改的帖子。于是在我自己摸索下,自己搞出来了,增删操作。接下来就分享给大家!界面布局:表格的tag:uitable1添加电价的tag:addEle删除电价的tag:delEle 首先建立一个newData.mat,用于存放表格数据: 在打开窗体的时候,加载newData.mat文件,…

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

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

界面布局:

表格的tag: uitable1

添加电价的tag:addEle

删除电价的tag:delEle

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

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

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

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

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

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

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

代码:

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

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

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

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

2.删除功能;

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

全部代码:

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.net/136639.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月3日 上午10:00
下一篇 2022年6月3日 上午10:00


相关推荐

  • 爬虫任务调度:APScheduler 定时执行

    爬虫任务调度:APScheduler 定时执行

    2026年3月14日
    1
  • 处理线上字典插入nil值引起的崩溃

    处理线上字典插入nil值引起的崩溃

    2022年4月2日
    37
  • phpexcel中文手册

    phpexcel中文手册首先到 phpexcel 官网上下载最新的 phpexcel 类 下周解压缩一个 classes 文件夹 里面包含了 PHPExcel php 和 PHPExcel 的文件夹 这个类文件和文件夹是我们需要的 把 classes 解压到你项目的一个目录中 重名名为 phpexcel 开始喽 代码都摘自自带实例 程序部分 require once phpexcel PHPExcel php 首先创建一个新的对象 PHPExcelobje objPHPExcel newPHP

    2026年3月18日
    2
  • Android 下拉刷新框架实现

    Android 下拉刷新框架实现一个通用的下拉刷新的框架介绍 前段时间项目中用到了下拉刷新功能 之前在网上也找到过类似的 demo 但这些 demo 的质量参差不齐 用户体验也不好 接口设计也不行 最张没办法 终于忍不了了 自己就写了一个下拉刷新的框架 这个框架是一个通用的框架 效果和设计感觉都还不错 现在分享给各位看官

    2026年3月20日
    2
  • 鸿蒙树莓派4b,树莓派4B

    鸿蒙树莓派4b,树莓派4B作者:长空无名最近很多科技媒体都报道了树莓派4发布的消息。虽然整个板子做了大幅升级,基础价格却依然是35美元,称的上是业界良心。那号称史上性能最强的树莓派4,到底有哪些亮点呢?一起来看产品图。USB-C供电口1.5GHz四核64位ARMCortex-A72CPU1GB/2GB/4GBLPDDR4SDRAM内存(可选)全吞吐量千兆以太网(真千兆)双频802.11ac无线网络蓝牙5…

    2022年4月30日
    61
  • non-compatible bean definition_cannot create a session after

    non-compatible bean definition_cannot create a session after前些天在我使用OkHttp的时候开始运行时出现了这么两个错误。错误一StaticinterfacemethodsareonlysupportedstartingwithAndroidN(–min-api24)大概意思就是静态接口方法只从AndroidN开始使用。错误二Invoke-customsareonlysupportedstartin…

    2025年9月17日
    11

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号