How to save the input received using Table in Matlab GUI?

11 views (last 30 days)
I am trying to develop an app that calculates the duration of construction activity, So I am trying to get input using UITable.
The user have to Enter the no of Actvities in the edit field and click Enter.
AN UITable is creted with required no of rows.
The user will input all the activities. These activities must be saved to an excel file.
So that the dates can be used for future referances. Can anyone help me?
I have added my script.
classdef Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ExportButton matlab.ui.control.Button
EditField matlab.ui.control.NumericEditField
EditFieldLabel matlab.ui.control.Label
EnterButton matlab.ui.control.Button
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: EnterButton
function EnterButtonPushed(app, event)
n = app.EditField.Value;
% Create table array
sz = [n 3];
varTypes = {'string','datetime','datetime'};
varNames = {'Activity','Start Date','End Date'};
t = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
% Create table UI component
uit = uitable(app.UIFigure);
uit.Data = t;
uit.ColumnEditable = [true true true];
uit.Position(3) = 310;
% Button pushed function: ExportButton
function ExportButtonPushed(app, event)
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create EnterButton
app.EnterButton = uibutton(app.UIFigure, 'push');
app.EnterButton.ButtonPushedFcn = createCallbackFcn(app, @EnterButtonPushed, true);
app.EnterButton.Position = [488 283 122 36];
app.EnterButton.Text = 'Enter';
% Create EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.Position = [448 367 55 22];
app.EditFieldLabel.Text = 'Edit Field';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'numeric');
app.EditField.Position = [518 353 92 49];
% Create ExportButton
app.ExportButton = uibutton(app.UIFigure, 'push');
app.ExportButton.ButtonPushedFcn = createCallbackFcn(app, @ExportButtonPushed, true);
app.ExportButton.Position = [503 212 119 29];
app.ExportButton.Text = 'Export';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 4 Apr 2022
Edited: Benjamin Kraus on 4 Apr 2022
As you are setting the Data property on the uitable using a MATLAB table, saving the resulting data to an Excel spreadsheet should be quite simple using the writetable command. To do that you need the handle to the uitable you create.
The preferred approach would be to add a property to your app that stores the handle to the uitable, but without that you can use findobj to get the handle back. Something like this:
uit = findobj(app.UIFigure,'Type','uitable');
writetable(uit.Data, 'filename.xlsx');
Benjamin Kraus
Benjamin Kraus on 4 Apr 2022
Edited: Benjamin Kraus on 4 Apr 2022
Three things:
  1. I don't think you can (or want to) set the DisplayData property like you are doing. I'm surprised that isn't generating an error.
  2. In the code above, you already have uit (a handle to your uitable) so the call to findobj isn't necessary.
  3. The much bigger issue is that you are calling writetable before you've given your users a chance to make any revisions to the table.
In your original app code you have an ExportButtonPushed callback that calls winopen. That is where you want to call writetable.
function ExportButtonPushed(app, event)
uit = findobj(app.UIFigure,'Type','uitable');
writetable(uit.Data, 'testdata.xlsx');

Sign in to comment.

More Answers (0)


Find more on Develop uifigure-Based Apps in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!