Hi. I have an a problem updating my excel file without overwriting the previous datas on the excel sheet. Am using a button to submit the input datas from matlab gui to the excel sheet.

4 views (last 30 days)
% this is the code but it is attached to a button in gui
filename = 'key_performance1';
serial_number = get(handles.sn,'string');
head_super = get(handles.hs,'string');
task_id = get(handles.t_id,'string');
user_sec = get(handles.u_s,'string');
tas = get(handles.tsk,'string');
solut = get(handles.sol,'string');
stat = get(handles.st,'string');
complt = get(handles.com,'string');
prio = get(handles.pri,'string');
date_of_en = get(handles.doe,'string');
estim_time = get(handles.es_t,'string');
due_day = get(handles.d_d,'string');
length_of = get(handles.lof,'string');
res_used = get(handles.ru,'string');
const = get(handles.con,'string');
asst = get(handles.ast,'string');
B = {'SERIAL NO','HEAD/SUPERVISOR','TASK ID','USER SECTION','TASK',...
'SOLUTION','STATUS','COMPLETE','PRIORITY','DATE OF ENTRY','ESTIMATED TIME OF RESPONSE',...
'DUE DAY','LENGHT OF RESPONSE','RESOURCES USED','CONSTRAINTS','ASSIGNED TO';serial_number,head_super,...
task_id,user_sec,tas,solut,stat,complt,prio,date_of_en,estim_time,due_day,length_of,res_used,const,asst};
sheet = 1;
xlRange = 'A';
row = 1;
if isfield(handles,'row')
row = handles.row;
end
xlswrite(filename,B,sheet,[xlRange num2str(row)]);
handles.row = row + 1;
  2 Comments
Afeez Oyedeji
Afeez Oyedeji on 5 Oct 2018
No it is not opened. The problem is not me not been able to write to the excel sheet, but whatever i write overwrites what has been on the excel sheet before

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 5 Oct 2018
Edited: Guillaume on 5 Oct 2018
Seems to me that the problem is simple: if you keep overwriting the same row that's because your row variable does not increase. The best way for you to find out why it does not increase is to use the debugger. Place a breakpoint at the start of your callback and see what happens.
I can see two possible reasons for why row does not increase:
  • The row is stored in handles.row and the first thing your code does is check that handles.row exist. Clearly, this means that there is the possibility that handles.row may not exist. In that case, row will always be 1. A well designed program would eliminate the possibility of handles.row not existing.
  • You store the next row in handle.rows, however your code doesn't show that handles get updated. It's unfortunate that you haven't shown the full code of the callback. Have you forgotten a guidata(hfig, handle) or whatever code is necessary to make sure handles is saved after you update it.
By the way, I'm a firm believer in not writing repeatedly similar lines of code and leaving the repetition to the computer. I would have written your code as:
filename = 'key_performance1';
control_names = {'sn', 'hs', 't', 'u_s', 'tsk', 'sol', 'st', 'com', 'pri', 'doe', 'es_t', 'd_d', 'lof', 'ru', 'con', 'ast'}; %fields of handles
%it would have been much better to name these fields, serial_number, head_super,
task_id, user_sec, etc...
control_values = cellfun(@(fn) handles.(fn), control_names, 'UniformOutput', false); %'UniformOutput', false is not needed if all values are numeric
headervalues = cell2table(control_values, 'VariableNames', ...
{'SERIAL NO','HEAD/SUPERVISOR','TASK ID','USER SECTION','TASK',...
'SOLUTION','STATUS','COMPLETE','PRIORITY','DATE OF ENTRY','ESTIMATED TIME OF RESPONSE',...
'DUE DAY','LENGHT OF RESPONSE','RESOURCES USED','CONSTRAINTS','ASSIGNED TO'})
if isfield(handles,'row')
row = handles.row;
else
row = 1;
end
writetable(headervalues, filename, 'Sheet', 1, 'Range', sprintf('A%d', row));
One thing that is puzzling with your code, which I have reproduced here, is that you write the header before each row, thus write two rows each time but only increase row by one. So on each write, you overwrite the second row written previously. You either need to increase row by 2 or write the header only for the 1st row.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!