I need help exporting data from table in Matlab GUI to excel

Hello everyone!
Hope all is well and in good health.
So where do i start?
I want to know if I created a 'Table' in GUI whereby a user inputs data - how will I go about exporting this table in a Excel Spreadsheet?
I read up on using xlswrite(filename,A,sheet) - How would I use this where appropiate?
For instance, for my homework - I need to make a table where 'Students' will input their grades and the 'data' will then export to Excel Spreadsheet for them to keep.
I'm awfully confused.
Any help would be appreciated.
Thank you.

 Risposta accettata

The attached app to see how you can create a GUI table in the app designer and save its content in an MS Excel file.

12 Commenti

Click on the code view at the top-right in the app designer window.
Thank you so much for this sample!
That it is exactly what i need
But how would i go about coding this?
Can i not execute this on Matlab GUI instead of App designer?
I used app designer: https://www.mathworks.com/help/matlab/app-designer.html to create the layout. You don't need coding to create such a layout, simply drag and drop the gaphical elements on the application canvas.
You can copy the code from the "code view" tab and paste it in a script. You will also need to remove 'app.' from that code. I wouldn't recommend going this route
UIFigure = uifigure('Visible', 'off');
UIFigure.Position = [100 100 640 480];
UIFigure.Name = 'MATLAB App';
% Create UITable
UITable = uitable(UIFigure);
UITable.ColumnName = {'Name'; 'Grade'};
UITable.RowName = {};
UITable.ColumnEditable = [true true];
UITable.Position = [181 198 302 185];
% Create filenameEditFieldLabel
filenameEditFieldLabel = uilabel(UIFigure);
filenameEditFieldLabel.HorizontalAlignment = 'right';
filenameEditFieldLabel.Position = [185 116 51 22];
filenameEditFieldLabel.Text = 'filename';
% Create filenameEditField
filenameEditField = uieditfield(UIFigure, 'text');
filenameEditField.Position = [251 116 100 22];
filenameEditField.Value = 'myExcelFile';
% Create SaveButton
SaveButton = uibutton(UIFigure, 'push');
% SaveButton.ButtonPushedFcn = createCallbackFcn(app, @SaveButtonPushed, true);
SaveButton.Position = [409 116 100 22];
SaveButton.Text = 'Save';
% Show the figure after all components are created
UIFigure.Visible = 'on';
Thank you so much for your help.
It's just im ONLY allowed to use Matlab GUI and NOT the app designer.
So how would i still do this?
Following code shows an example of how to do it without app designer. To futher modify the layout, read the documentation of uifigure, uitable, uieditfield, etc.
UIFigure = uifigure('Visible', 'off');
UIFigure.Position = [100 100 640 480];
UIFigure.Name = 'MATLAB App';
% Create UITable
UITable = uitable(UIFigure);
UITable.ColumnName = {'Name'; 'Grade'};
UITable.RowName = {};
UITable.ColumnEditable = [true true];
UITable.Position = [181 198 302 185];
% Create filenameEditFieldLabel
filenameEditFieldLabel = uilabel(UIFigure);
filenameEditFieldLabel.HorizontalAlignment = 'right';
filenameEditFieldLabel.Position = [185 116 51 22];
filenameEditFieldLabel.Text = 'filename';
% Create filenameEditField
filenameEditField = uieditfield(UIFigure, 'text');
filenameEditField.Position = [251 116 100 22];
filenameEditField.Value = 'myExcelFile';
% Create SaveButton
SaveButton = uibutton(UIFigure, 'push');
SaveButton.ButtonPushedFcn = {@SaveButtonPushed, UITable, filenameEditField};
SaveButton.Position = [409 116 100 22];
SaveButton.Text = 'Save';
% Show the figure after all components are created
UIFigure.Visible = 'on';
t = table(repmat({''}, 10, 1), zeros(10,1));
UITable.Data = t;
function SaveButtonPushed(~, ~, table, editField)
filename = [editField.Value '.xlsx'];
t = table.Data;
t.Properties.VariableNames = table.ColumnName;
writetable(t, filename);
end
Thank you again.
Will I need to manually put a table on GUI then copy and paste this code?
Just paste this code in a script file and run that file. It will create the GUI.
Under which function exactly?
function varargout = save_document_OutputFcn(hObject, eventdata, handles)
How did you create this line
function varargout = save_document_OutputFcn(hObject, eventdata, handles)
Are you using GUIDE?
How to export colored UITable data to excel? The code above works but I want to colored specific cells and export all data with colored. There must be a way to get colored excel sheet from UI table.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by