MATLAB Answers

1

Problem with copyfile/movefile, xlswrite and Excel 2013

Asked by Michael Schmid on 22 Dec 2016
Latest activity Commented on by Walter Roberson
on 1 Jan 2017
Hello MATLAB geeks out there. I stumbled across a problem I just can't solve.
Task is simple. Create a folder in a dynamic directory, copy a already existing blueprint.xls from a static directory to the new directory and name it dynamically. Then do fill in some values at specific locations of that file. (It is a measurement protocol) Now the witchcraft starts happening. If I run my creating-data-code and the write-to-xls-code in a stand alone program it works perfectly. When I debug and execute step by step, the code in the main .m file also works. But when I want it to run normally I always get an Errormessage (not from Matlab itself) that something happened and it tries to restore the file (which works) the code does not interrupt, the message just pops up as often as I write into the xls file. When the execution has finished, and I open the file everything is just as it should be. All values are where they should be. So why do I run into this problem? This is the code i use:
plate_type='testitest';
setting='S1';
number_of_plates='4';
filename=[datestr(now,'yymmdd') '_' plate_type '_' 'Setting_' setting '_' 'NR_1-' number_of_plates];
%------------------------------------------------------
folder_directory=strrep('C:\Users\Smi-Laptop\Desktop','\','/');
mkdir(folder_directory,filename);
%-------------------------------------------------
filelocation=strrep(['C:\Users\Smi-Laptop\Desktop' '/' filename],'\','/');
copyfile('Blueprint.xls',filelocation);
cd(filelocation);
movefile('Blueprint.xls',[filename '.xls']);
%----------------------------------------------------
ring_count=4;
plate_count=3;
cd('C:/Users/Public/Documents/burster/DigiControl 9310/Data/2016/1219');
for m=1:1:ring_count
for n=1:1:plate_count
load(listbox_plate{n});
ring_number=['ring' num2str(m)];
F_max_abs(n)=ring_data.(ring_number).F_max;
F_min_abs(n)=ring_data.(ring_number).F_min;
F_avg(n)=ring_data.(ring_number).F_avg;
end
F_max_abs_set=max(F_max_abs);
F_min_abs_set=min(F_min_abs);
F_avg_set=mean(F_avg);
F_std_set=std(F_max_abs);
protocol_ring{1,:}=F_max_abs_set;
protocol_ring{2,:}=F_min_abs_set;
protocol_ring{3,:}=0;
protocol_ring{4,:}=F_avg_set;
protocol_ring{5,:}=F_std_set;
protocol_ring{6,:}=0;
switch m
case 1
write_position='D14';
case 2
write_position='I14';
case 3
write_position='D23';
case 4
write_position='I23';
case 5
write_position='D32';
case 6
write_position='I32';
case 7
write_position='D41';
case 8
write_position='I41';
end
% write_position='D14';
xlswrite([filelocation '/' filename '.xls'], protocol_ring, 'Sheet1', write_position);
clearvars protocol_ring F_max_abs F_avg;
end
cd(orig_pathname);
apart from the fact that this standalone code does not work without some input variables (blueprint.xls, proper file location and listbox_plate, ring_data(struct)) it does work fine for me in a standalone program but not in the .m file (this code is a callback in an UI, the callback does not contain any more code) I hope you can help me figure out why this error message keeps popping up, slows everything down but everything still works. Thank you very much!

  1 Comment

Which MATLAB version are you using?

Sign in to comment.

1 Answer

Answer by Rylan Dmello on 28 Dec 2016

Hello Michael, this may occur due to the multiple xlswrite commands that are being executed in a for loop. MATLAB may use a single instance of Excel to write the .xls files, and if it is not completed writing the file during each iteration of the loop, then it might show an error message when the next xlswrite command is called.
Try using the pause command right after the xlswrite command to pause execution of the for loop for some milliseconds while MATLAB writes to the Excel file. Alternatively, you could also try writing the Excel file using the writetable command.

  1 Comment

Hello Rylan, thank you for your answer and a happy new year! I also thought about that and I already tried pausing for 5 seconds but it still wont work.

Sign in to comment.