MATLAB Answers

0

I have created a GUI to take some data from user and save this input in EXCEL file, but every time when user inputs the data and hits submit button the data is overwritten.

Asked by Shishir Agrahari on 13 Jul 2018
Latest activity Commented on by Dennis
on 19 Jul 2018
I thought a logic that every time when user hit submit button, matlab should check for the empty row and and write data in that row. Please suggest some code for this. The code i have used is as follows, but it is not working. n=100; for j= 1:n if(Aj=='') xlswrite('test2.xlsx',data,Aj:Cj); else j=j+1; end

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Dennis
on 13 Jul 2018

Aj cannot work, you are hoping that MATLAB takes A (not a string, probably doesnt even exist) and j (a double) and put them together to form a string.
I am not sure what the loop and the if statement are supposed to do if you want to write your data via GUI i suggest you take a variable j and pass it to your callback to keep track of rowcount.
To make your actual code work you need to tell MATLAB what to do with A,C and j.
xlswrite('test2.xlsx',data,1,['A',num2str(j),':','C',num2str(j)])

  6 Comments

You can use xlsread() and check how many lines are already written.
function myGUI()
handles.pb=uicontrol('Style','pushbutton','String','Submit','Callback',@submit_callback);
[~,~,raw]=xlsread('test2.xlsx');
j=size(raw,1)+1;
data={'I', 'love', 'cookies'};
setappdata(handles.pb,'data',data)
setappdata(handles.pb,'j',j)
end
<<
>> Hello ! Dennis , Here is what my GUI looks like, and i want to store the data in separate excel sheet according to size in same excel file. my code looks like:
function varargout = tshirt_test(varargin) % TSHIRT_TEST MATLAB code for tshirt_test.fig % TSHIRT_TEST, by itself, creates a new TSHIRT_TEST or raises the existing % singleton*. % % H = TSHIRT_TEST returns the handle to a new TSHIRT_TEST or the handle to % the existing singleton*. % % TSHIRT_TEST('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in TSHIRT_TEST.M with the given input arguments. % % TSHIRT_TEST('Property','Value',...) creates a new TSHIRT_TEST or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before tshirt_test_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to tshirt_test_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 tshirt_test
% Last Modified by GUIDE v2.5 12-Jul-2018 08:49:24
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @tshirt_test_OpeningFcn, ... 'gui_OutputFcn', @tshirt_test_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 tshirt_test is made visible. function tshirt_test_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to tshirt_test (see VARARGIN)
% Choose default command line output for tshirt_test handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes tshirt_test wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = tshirt_test_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;
function [theMessage, TargetSheet, visibility] = activate_sheet(Excel,Sheet) % Activate specified worksheet in workbook.
% Initialize worksheet object WorkSheets = Excel.sheets; theMessage = struct('message',{''},'identifier',{''});
function name_Callback(hObject, eventdata, handles) % hObject handle to name (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 name as text % str2double(get(hObject,'String')) returns contents of name as a double
% --- Executes during object creation, after setting all properties. function name_CreateFcn(hObject, eventdata, handles) % hObject handle to name (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 reg_Callback(hObject, eventdata, handles) % hObject handle to reg (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 reg as text % str2double(get(hObject,'String')) returns contents of reg as a double
% --- Executes during object creation, after setting all properties. function reg_CreateFcn(hObject, eventdata, handles) % hObject handle to reg (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 contact_Callback(hObject, eventdata, handles) % hObject handle to contact (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 contact as text % str2double(get(hObject,'String')) returns contents of contact as a double
% --- Executes during object creation, after setting all properties. function contact_CreateFcn(hObject, eventdata, handles) % hObject handle to contact (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 room_Callback(hObject, eventdata, handles) % hObject handle to room (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 room as text % str2double(get(hObject,'String')) returns contents of room as a double
% --- Executes during object creation, after setting all properties. function room_CreateFcn(hObject, eventdata, handles) % hObject handle to room (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 on selection change in mode. function mode_Callback(hObject, eventdata, handles) % hObject handle to mode (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns mode contents as cell array % contents{get(hObject,'Value')} returns selected item from mode
% --- Executes during object creation, after setting all properties. function mode_CreateFcn(hObject, eventdata, handles) % hObject handle to mode (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 pid_Callback(hObject, eventdata, handles) % hObject handle to pid (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 pid as text % str2double(get(hObject,'String')) returns contents of pid as a double
% --- Executes during object creation, after setting all properties. function pid_CreateFcn(hObject, eventdata, handles) % hObject handle to pid (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 on selection change in size. function size_Callback(hObject, eventdata, handles) % hObject handle to size (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns size contents as cell array % contents{get(hObject,'Value')} returns selected item from size
% --- Executes during object creation, after setting all properties. function size_CreateFcn(hObject, eventdata, handles) % hObject handle to size (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 on button press in submit. function submit_Callback(hObject, eventdata, handles) % hObject handle to submit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) names = get(handles.name,'string'); regs = str2num(get(handles.reg,'string')); contacts = str2num(get(handles.contact,'string')); rooms = str2num(get(handles.room,'string')); modes = get(handles.mode,'value'); switch modes case 1 msgbox('Select Payment Mode'); case 2 payment_mode = 'PayTM'; case 3 payment_mode = 'PhonePe'; case 4 payment_mode = 'Tez'; case 5 payment_mode = 'BhimUPI'; case 6 payment_mode = 'Cash'; end pids = get(handles.pid,'string');
sizes = get(handles.size,'value'); switch sizes case 1 msgbox('Select Size'); case 2 sizem = 'S'; data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size';names,regs,contacts,rooms, payment_mode,pids,sizem}; xlswrite('matlab.xlsx',data,'S'); case 3 sizem = 'M'; data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size';names,regs,contacts,rooms, payment_mode,pids,sizem}; xlswrite('matlab.xlsx',data,'M'); case 4 sizem = 'L'; data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size';names,regs,contacts,rooms, payment_mode,pids,sizem}; xlswrite('matlab.xlsx',data,'L'); case 5 sizem = 'XL'; data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size';names,regs,contacts,rooms, payment_mode,pids,sizem}; xlswrite('matlab.xlsx',data,'XL'); case 6 sizem = 'XXL'; data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size';names,regs,contacts,rooms, payment_mode,pids,sizem}; xlswrite('matlab.xlsx',data,'XXL'); case 7 sizem = 'XXXL'; data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size';names,regs,contacts,rooms, payment_mode,pids,sizem}; xlswrite('matlab.xlsx',data,'XXXL'); end msgbox('Details Successfully Submitted'); set(handles.name,'String',''); set(handles.reg,'String',''); set(handles.contact,'String',''); set(handles.room,'String',''); set(handles.pid,'String',''); set(handles.mode,'Value',1); set(handles.size,'Value',1);
% --- Executes on button press in clear. function clear_Callback(hObject, eventdata, handles) % hObject handle to clear (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.name,'String',''); set(handles.reg,'String',''); set(handles.contact,'String',''); set(handles.room,'String',''); set(handles.pid,'String',''); set(handles.mode,'Value',1); set(handles.size,'Value',1);

Sign in to comment.


Answer by Dennis
on 14 Jul 2018

I don't have much time to test right now. This might work:
sizes = get(handles.size,'value');
switch sizes
case 1
msgbox('Select Size');
case 2
sizem = 'S';
case 3
sizem = 'M';
case 4
sizem = 'L';
case 5
sizem = 'XL';
case 6
sizem = 'XXL';
case 7
sizem = 'XXXL';
end
[~,~,raw]=xlsread('matlab.xlsx',sizem);
j=size(raw,1)+1;
data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size';names,regs,contacts,rooms, payment_mode,pids,sizem};
xlswrite('test2.xlsx',data,sizem,['A',num2str(j),':','C',num2str(j+1)])
msgbox('Details Successfully Submitted');
set(handles.name,'String','');
set(handles.reg,'String','');
set(handles.contact,'String','');
set(handles.room,'String','');
set(handles.pid,'String','');
set(handles.mode,'Value',1);
set(handles.size,'Value',1);

  5 Comments

Dennis its still not working..please give me your mail i will send you the complete figure file and .m file so that you can make appropriate changes and check.
Can you explain which part is not working? I just ran a simple test and for me it runs fine. You can use the paperclip button to attach files here.
for i=1:50
sizes=randi(7);
switch sizes
case 1
msgbox('Select Size');
case 2
sizem = 'S';
case 3
sizem = 'M';
case 4
sizem = 'L';
case 5
sizem = 'XL';
case 6
sizem = 'XXL';
case 7
sizem = 'XXXL';
end
try
[~,~,raw]=xlsread('matlab.xlsx',sizem);
j=size(raw,1)+1;
catch
data = {'Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size'};
xlswrite('matlab.xlsx',data,sizem,['A1:G1'])
j=2;
end
%some test data
names=char(63+j);
regs=char(63+j);
contacts=char(63+j);
rooms=char(63+j);
payment_mode=char(63+j);
pids=char(63+j);
data={names,regs,contacts,rooms, payment_mode,pids,sizem};
xlswrite('matlab.xlsx',data,sizem,['A',num2str(j),':','G',num2str(j)])
% msgbox('Details Successfully Submitted');
end

Sign in to comment.