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.
이전 댓글 표시
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
답변 (2개)
Dennis
2018년 7월 13일
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
Shishir Agrahari
2018년 7월 13일
Dennis
2018년 7월 13일
If you used a logic to check for empty cells you did not share that part of your code.
Can you specify what is not working with my line, it runs just fine for me.
I do not know how you pass your arguments around in your GUI - so i can only guess how your callback should look like.
function myGUI()
handles.pb=uicontrol('Style','pushbutton','String','Submit','Callback',@submit_callback);
data={'I', 'love', 'cookies'};
setappdata(handles.pb,'data',data)
end
function submit_callback(hObj,~)
j=getappdata(hObj,'j');
data=getappdata(hObj,'data');
if isempty(j)==1
j=1;
end
xlswrite('test2.xlsx',data,1,['A',num2str(j),':','C',num2str(j)])
j=j+1;
setappdata(hObj,'j',j)
end
Shishir Agrahari
2018년 7월 13일
Dennis
2018년 7월 13일
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
Shishir Agrahari
2018년 7월 14일
Shishir Agrahari
2018년 7월 14일
Dennis
2018년 7월 14일
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
Shishir Agrahari
2018년 7월 15일
Read from one file and wrote to another - corrected version:
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
j=1;
end
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,sizem,['A',num2str(j),':','G',num2str(j+1)])
msgbox('Details Successfully Submitted');
You want the header ('Name','Registration Number','Contact Number','Room Number','Payment Mode','Payment ID','Size') written every time or only in first line?
Shishir Agrahari
2018년 7월 17일
Shishir Agrahari
2018년 7월 18일
Dennis
2018년 7월 19일
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
카테고리
도움말 센터 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

