How do you correctly save an excel workbook using ActiveX?

2 views (last 30 days)
Hey there, trying to finish some code that uses some loops to read excel documents, compute some stuff in matlab, and write some images to new documents. Everytime I try to save the document I just opened, I get an error message. Here's my code at the moment.
for i = 1:1 %increments files
for j = 1:2 %increments sheets
sheet = j; %assigns sheet names
bob = sprintf('File%d.xlsx',i);
jane = sprintf('Output%d.xlsx',i);
matrix = xlsread(bob,sheet); %reads from sheet
nm = matrix(:,1);
I = matrix(:,2);
nI = I-min(I);
gauss = @(p,nm) p(1).*exp((-1*(nm-p(2)).^2)./((2*p(3).^2)));
guess2 = [100000 570 1];
g = fitnlm(nm,nI,gauss,guess2);
ng = feval(g,nm);
plot(nm, nI, '-', nm, ng, 'x');
img = 'figure1.png'; %create image
print('-dpng', img); %copy it to clipboard
beta = g.Coefficients.Estimate;
nmmax = beta(2);
intensity = beta(1);
Iter = {'N1', 'N2', 'N3', 'N4'}';
Head = {'Lambda Max', 'Max Intensity'};
cellB = sprintf('B%d:B%d',j+1,j+1);
cellC = sprintf('C%d:C%d',j+1,j+1);
xlswrite(jane,Head,'B1:C1'); %prints column headers
xlswrite(jane,Iter,'A2:A5'); %prints row labels
xlswrite(jane,nmmax,cellB); %prints lambda max
xlswrite(jane,intensity,cellC); %prints maximum intensity
Excel = actxserver('excel.application');
set(Excel,'Visible',1);
currentdir = cd;
filepath = [currentdir '\' jane];
Workbook = Excel.Workbooks;
invoke(Workbook,'Open',filepath);
Sheets = Excel.ActiveWorkBook.Sheets;
sheet = get(Sheets,'Item',1);
invoke(sheet,'Activate');
Shapes = sheet.Shapes;
if (j == 1);
Shapes.AddPicture([pwd '\' img], 0,1,10,80,350,275);
end
if (j == 2);
Shapes.AddPicture([pwd '\' img], 0,1,10,160,350,275);
end
invoke(Excel, 'Save', filepath);
invoke(Excel, 'Quit');
end
end
I've tried using filepath and jane, to signify what I want to save, but neither of these work!

Answers (1)

Walter Roberson
Walter Roberson on 27 Aug 2015
You might want to look at xlswrite1() in the File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!