How to populate uitable from pop-up menu, and edit text field with push button in Guide

5 views (last 30 days)
I am trying to create a GUI in guide with a table, pop-up menu, edit text field, and a push button. The goal is to have someone select an option from the pop up menu, add a comment in the edit text field and then press the push button. The push button would then populate the table with the pop up menu selection, the comment, and the time it was selected. Unfortunately I have not been able to figure out how to update the table with the other GUI inputs. Help would be greatly appreciated. Thank you.
function varargout = CWTB_TimeSync(varargin)
% CWTB_TIMESYNC MATLAB code for CWTB_TimeSync.fig
% CWTB_TIMESYNC, by itself, creates a new CWTB_TIMESYNC or raises the existing
% singleton*.
%
% H = CWTB_TIMESYNC returns the handle to a new CWTB_TIMESYNC or the handle to
% the existing singleton*.
%
% CWTB_TIMESYNC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CWTB_TIMESYNC.M with the given input arguments.
%
% CWTB_TIMESYNC('Property','Value',...) creates a new CWTB_TIMESYNC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before CWTB_TimeSync_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to CWTB_TimeSync_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 CWTB_TimeSync
% Last Modified by GUIDE v2.5 09-Apr-2019 13:40:02
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @CWTB_TimeSync_OpeningFcn, ...
'gui_OutputFcn', @CWTB_TimeSync_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 CWTB_TimeSync is made visible.
function CWTB_TimeSync_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 CWTB_TimeSync (see VARARGIN)
% Choose default command line output for CWTB_TimeSync
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes CWTB_TimeSync wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = CWTB_TimeSync_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 selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(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) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
data = get(hObject, 'data');
%disp(data);
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
c = clock; % Grabs time button is pushed
items = get(handles.popupmenu1,'String'); % Gets event selected
index_selected = get(handles.popupmenu1,'Value');
item_selected = items{index_selected};
inputE1 = char(get(handles.edit1,'String')); % Gets Comment inputted
set(handles.uitable1, 'Event', items);
data = get(handles.uitable1, 'Data');% Adds another blank row
data(end+1,:) = '';
set(handles.uitable1, 'Data', data);
newValue = ['']; % Resets comment box
set(handles.edit1,'String',newValue);

Answers (1)

TED MOSBY
TED MOSBY on 4 Jul 2025
Hi,
You can create the figure and controls programatically and add a button callback that will:
  • extracts the chosen item and comment,
  • time-stamps with datestr(now,...),
  • appends a new cell row to the existing table data,
  • clears the edit box.
Because everything lives inside the same function, we can simply stash control handles with guidata and retrieve them inside the callback as below:
function logEventsGUI
fig = figure('Name','Event logger demo', ...
'MenuBar','none', ...
'NumberTitle','off', ...
'Position',[100 100 560 300]);
uicontrol(fig,'Style','text', ...
'String','Event type:', ...
'Position',[20 260 70 20], ...
'HorizontalAlignment','left');
popup = uicontrol(fig,'Style','popupmenu', ...
'String',{'Start-Up','Stop','Fault','Reset'}, ...
'Position',[90 260 100 22]);
uicontrol(fig,'Style','text', ...
'String','Comment:', ...
'Position',[210 260 60 20], ...
'HorizontalAlignment','left');
edit = uicontrol(fig,'Style','edit', ...
'Position',[270 260 190 24], ...
'BackgroundColor','white');
btn = uicontrol(fig,'Style','pushbutton', ...
'String','Add', ...
'Position',[470 258 60 28], ...
'Callback',@onAddPressed);
tbl = uitable(fig, ...
'Data',cell(0,3), ...
'ColumnName',{'Event','Comment','Time'}, ...
'ColumnEditable',[false false false], ...
'Position',[20 20 510 220]);
handles.popup = popup;
handles.edit = edit;
handles.tbl = tbl;
guidata(fig,handles);
function onAddPressed(~,~)
h = guidata(gcf); % retrieve handles
items = h.popup.String;
ev = items{ h.popup.Value };
note = h.edit.String;
ts = datestr(now,'yyyy-mm-dd HH:MM:SS');
oldData = h.tbl.Data;
h.tbl.Data = [oldData ; {ev, note, ts}];
h.edit.String = '';
end
end
This outputs a GUI like this:
Here is more info on "guidata" :
Hope it helps!

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!