GUI table data into excel sheet

12 views (last 30 days)
AYUSH ANAND SAGAR
AYUSH ANAND SAGAR on 25 Oct 2020
Answered: Tarunbir Gambhir on 30 Oct 2020
function varargout = attendancetable_1(varargin)
% ATTENDANCETABLE_1 MATLAB code for attendancetable_1.fig
% ATTENDANCETABLE_1, by itself, creates a new ATTENDANCETABLE_1 or raises the existing
% singleton*.
%
% H = ATTENDANCETABLE_1 returns the handle to a new ATTENDANCETABLE_1 or the handle to
% the existing singleton*.
%
% ATTENDANCETABLE_1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ATTENDANCETABLE_1.M with the given input arguments.
%
% ATTENDANCETABLE_1('Property','Value',...) creates a new ATTENDANCETABLE_1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before attendancetable_1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to attendancetable_1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help attendancetable_1
% Last Modified by GUIDE v2.5 25-Oct-2020 22:05:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @attendancetable_1_OpeningFcn, ...
'gui_OutputFcn', @attendancetable_1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before attendancetable_1 is made visible.
function attendancetable_1_OpeningFcn(hObject, eventdata, handles, varargin)
global p
p.MyData=[];
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes attendancetable_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = attendancetable_1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global p
NameData = get(handles.NameField, 'string');
RegisterNumberFieldData = get(handles.RegisterNumberField, 'string');
p.MyData= [p.MyData ; [{NameData} {RegisterNumberFieldData}]];
set(handles.TableField, 'Data',p.MyData)
function NameField_Callback(hObject, eventdata, handles)
% hObject handle to NameField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of NameField as text
% str2double(get(hObject,'String')) returns contents of NameField as a double
% --- Executes during object creation, after setting all properties.
function NameField_CreateFcn(hObject, eventdata, handles)
% hObject handle to NameField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function RegisterNumberField_Callback(hObject, eventdata, handles)
% hObject handle to RegisterNumberField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of RegisterNumberField as text
% str2double(get(hObject,'String')) returns contents of RegisterNumberField as a double
% --- Executes during object creation, after setting all properties.
function RegisterNumberField_CreateFcn(hObject, eventdata, handles)
% hObject handle to RegisterNumberField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes when entered data in editable cell(s) in TableField.
function TableField_CellEditCallback(hObject, eventdata, handles)
% hObject handle to TableField (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Print.
function Print_Callback(hObject, eventdata, handles)
global p
filename='Students.xlsx';
xlswrite(filename,p);
p=p+1;
% Print = get(handles.TableField, 'string');
%%Errors
>> attendancetable_1
Error using xlswrite (line 170)
Input data must be a numeric, cell, or logical array.
Error in attendancetable_1>Print_Callback (line 145)
xlswrite(filename,p);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in attendancetable_1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)attendancetable_1('Print_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

Answers (1)

Tarunbir Gambhir
Tarunbir Gambhir on 30 Oct 2020
To use "xlswrite" your data must be of type specified in the error description. When you use "p.MyData" to store your data, variable "p" becomes a structure.
To store this data you should use
xlswrite(filename,p.MyData)
Also, using an empty array to initialize your data might throw an error if the input array for xlswrite remains empty. A workaround to this could be to initialize "p.MyData" as
p.MyData = [NaN];
This would create an empty excel sheet as MATLAB will convert NaN to empty cell. You can find more information here.
Note: If you are using MATLAB R2019a or later, it is recommended that you use writetable, writematrix, or writecell instead of xlswrite.

Community Treasure Hunt

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

Start Hunting!