MATLAB GUI表格(uitable)的增刪操作
阿新 • • 發佈:2018-12-11
這幾天,查看了很多的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); %更新結構體