This video shows the code to save the app.UITable.Data to a Microsoft Excel file from the UI designed in MATLAB App Designer R2023a.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
Main Methods:
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
table_data = [1 5 2 3; 2 3 9 8; 0 2 4 5];
set(app.UITable, 'data', table_data);
end
% Button pushed function: ExporttoExcelFileButton
function ExporttoExcelFileButtonPushed(app, event)
writematrix(app.UITable.Data, 'TableData.xls');
end
end
Complete Code:
classdef UITableToExcel < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ExporttoExcelFileButton matlab.ui.control.Button
UITable matlab.ui.control.Table
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
table_data = [1 5 2 3; 2 3 9 8; 0 2 4 5];
set(app.UITable, 'data', table_data);
end
% Button pushed function: ExporttoExcelFileButton
function ExporttoExcelFileButtonPushed(app, event)
writematrix(app.UITable.Data, 'TableData.xls');
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 UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
app.UITable.RowName = {'Row1'; 'Row2'; 'Row3'};
app.UITable.ColumnSortable = true;
app.UITable.ColumnEditable = true;
app.UITable.Position = [38 200 573 225];
% Create ExporttoExcelFileButton
app.ExporttoExcelFileButton = uibutton(app.UIFigure, 'push');
app.ExporttoExcelFileButton.ButtonPushedFcn = createCallbackFcn(app, @ExporttoExcelFileButtonPushed, true);
app.ExporttoExcelFileButton.Position = [266 126 118 23];
app.ExporttoExcelFileButton.Text = 'Export to Excel File';
% 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 = UITableToExcel
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
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
Screenshots: