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

30 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
end
% 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;
end
% Button pushed function: ExportButton
function ExportButtonPushed(app, event)
winopen('testdata.xlsx');
end
end
% 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';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

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');
  5 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by