How to save the input received using Table in Matlab GUI?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Raja Suriyan
el 4 de Abr. de 2022
Comentada: Raja Suriyan
el 4 de Abr. de 2022
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
0 comentarios
Respuesta aceptada
Benjamin Kraus
el 4 de Abr. de 2022
Editada: Benjamin Kraus
el 4 de Abr. de 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 comentarios
Benjamin Kraus
el 4 de Abr. de 2022
Editada: Benjamin Kraus
el 4 de Abr. de 2022
Three things:
- 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.
- In the code above, you already have uit (a handle to your uitable) so the call to findobj isn't necessary.
- 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');
winopen('testdata.xlsx');
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!