I keep getting the error, "Unable to write to file 'xxxx.xlsx'. You may not have write permissions or the file may be open by another application."

I keep receiving this error when attempting to write tables to Excel from MATLAB.
%% Velocity Tables
T1 = table(vx1,'VariableNames',{'CummulativeVelocity'});
T2 = table(vy1,'VariableNames',{'CummulativeVelocity'});
T3 = table(vz1,'VariableNames',{'CummulativeVelocity'});
T4 = table(vx2,'VariableNames',{'CummulativeVelocity'});
T5 = table(vy2,'VariableNames',{'CummulativeVelocity'});
T6 = table(vz2,'VariableNames',{'CummulativeVelocity'});
T7 = table(vx3,'VariableNames',{'CummulativeVelocity'});
T8 = table(vy3,'VariableNames',{'CummulativeVelocity'});
T9 = table(vz3,'VariableNames',{'CummulativeVelocity'});
T10 = table(vx4,'VariableNames',{'CummulativeVelocity'});
T11 = table(vy4,'VariableNames',{'CummulativeVelocity'});
T12 = table(vz4,'VariableNames',{'CummulativeVelocity'});
filename = 'Velocity2.xlsx';
writetable(T1,filename,'Sheet',1,'Range','A1');
writetable(T2,filename,'Sheet',1,'Range','C1');
writetable(T3,filename,'Sheet',1,'Range','E1');
writetable(T4,filename,'Sheet',2,'Range','A1');
writetable(T5,filename,'Sheet',2,'Range','C1');
writetable(T6,filename,'Sheet',2,'Range','E1');
writetable(T7,filename,'Sheet',3,'Range','A1');
writetable(T8,filename,'Sheet',3,'Range','C1');
writetable(T9,filename,'Sheet',3,'Range','E1');
writetable(T10,filename,'Sheet',4,'Range','A1');
writetable(T11,filename,'Sheet',4,'Range','C1');
writetable(T12,filename,'Sheet',4,'Range','E1');
Here is the code I am using. The code works fine for the first instance of writetable, but the error will come up when attempting to run any other instance. I have found that I can manually end the Excel process in Task Manager, but this has to be done every time, while also creating a second copy of the worksheet. This second worksheet is the one that has the new table written in it. So, in order to continue the process I have to rename the copy and delete the previous version.
There must be a way to either better write this code (I am still pretty green), or to add something to fix this error. I don't kow what either of these solutions would be. Also, Excel is closed for each run of writetable.

2 Commenti

A few things...
  1. Don't dynamically name variables in workspace
  2. You don't want to be calling writetable repeatedly, it should all be done in one operation.
  3. Where ever you have multiple repeated lines like that, there is an easier way
Can you show the code you used to create vx1, vy1 vz1 etc. Lets get that in to better format then deal with creating the xlsx.
Here is the code used to create all velocity vectors. They come from raw data from an accelerometer.
%% Integration to Velocity
% 480 CFH
vx1 = cumtrapz(ax1);
vy1 = cumtrapz(ay1);
vz1 = cumtrapz(az1);
% 960 CFH
vx2 = cumtrapz(ax2);
vy2 = cumtrapz(ay2);
vz2 = cumtrapz(az2);
% 2580 CFH
vx3 = cumtrapz(ax3);
vy3 = cumtrapz(ay3);
vz3 = cumtrapz(az3);
% 3600 CFH
vx4 = cumtrapz(ax4);
vy4 = cumtrapz(ay4);
vz4 = cumtrapz(az4);

Accedi per commentare.

 Risposta accettata

I've updated the answer to resolve your specific issue. Though I would suggest doing all processing in MATLAB not excel since that is what its for. With this solution all the data is stored in a cell array idt. Each cell idt{1} for example contains 1 set of test data from the accel.
idt{1} = [ax1 ay1 az1]; % quick fix - but try import your data in to one variable, rather than directly in to workspace.
idt{2} = [ax2 ay2 az2];
idt{3} = [ax3 ay3 az3];
idt{4} = [ax4 ay4 az4];
for k = 1:4
res{k} = cumtrapz(idt{k}(:,:))
res{k} = array2table(res{k}) % Convert data to table
res{k}.Properties.VariableNames = {'a1_X', 'a1_Y' 'a1_Z'} % Assign variable names for columns
writetable((res{k}),'results.xlsx','sheet',k) % Writetable to excel sheet
end
Without exporting to excel all you require is:
idt{1} = [ax1 ay1 az1]; % quick fix - but try import your data in to one variable, rather than directly in to workspace.
idt{2} = [ax2 ay2 az2];
idt{3} = [ax3 ay3 az3];
idt{4} = [ax4 ay4 az4];
for k = 1:4
res{k} = cumtrapz(idt{k}(:,:))
end
Ask a question regarding calculating velocity here and we can have a look.

5 Commenti

One issue is that each trial of accelerations (a1, a2, a3, a4) have varying sizes. Each x, y, and z from each trial are equal, but in total they are different. I also do a calculation once initially exported to Excel to transform the cummulative integration of velocity to individual velocitites. I am plotting each step vs time, so a cummulative number doesn't mean much.
I must do this process again to obtain displacement as well. This probably should've been stated in my original post. My apologies.
I'll rectify my answer shortly to allow for the different lengths.
Why do you want to do the velocity calculations in excel? They can be done in MATLAB with relative ease. Though I do warn that calculating velocity from accels is fraught with danger due to drift, twist and gravity.
I have a stationary, let's call it, "arm" that is submerged in water and I need to see how much it displaces based on different airflows through said arm. We are doing a somewhat crude vibration analysis to see if the activity of the arm is large enough to break a connection.
I do the initial integration of velocity in MATLAB, but the only numerical function I could find, with my limited knowledge, is cumtrapz which returns an array with each step added to the previous, so I use excel to then take the difference between two steps to calculate the velocity and later displacement at each time instance.
I'm sure I could do this calculation with a for loop, but my knowledge base isn't there yet, so I've done this sort of brute force method instead.
Updated answer, please accept.
Ask a new question on completing the rest of your analysis in MATLAB.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by