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.
1 次查看(过去 30 天)
显示 更早的评论
% 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 个评论
Raghunandan V
2018-10-5
Is your excel file open? Because you cant write in to excel file when the excel is open
采纳的回答
Guillaume
2018-10-5
编辑:Guillaume
2018-10-5
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!