Azzera filtri
Azzera filtri

Error using xlswrite1 in a For loop?

2 visualizzazioni (ultimi 30 giorni)
GMabilleau
GMabilleau il 24 Apr 2017
Risposto: GMabilleau il 18 Mag 2017
Hi everyone!
I wrote a script that is supposed to work in a batch mode to recognise all files located in the same folder with a specific extension (in my case '.0'). This script opens a file at a time with a proprietary function (called 'ImportFile), perform some processing and measurements (around 20) and save all these measurements in an excel file that has the same name than the original file, apart the extension ('.0' is replace by '.xls'). Part of the code is written below, with '...' being the processing I made. My problem is to save the data into an excel format. Due to the redundancy in saving data, I decided not to use xlswrite but rather xlswrite1. Here is the code I used:
cd('DATA');
files = dir('*.0');
for k = 1:numel(files)
[intensity,wvn,params] = ImportFile(files(k).name);
...
% these calculations give raise to the following variables: param1, param2, param3 and param4
input_name = files(k).name;
[path,name,extension] = fileparts(input_name);
xlssave = fullfile(path,['Results_' name '.xls']);
Excel = actxserver('Excel.Application');
ExcelWorkbook = Excel.workbooks.Add;
ExcelWorkbook.SaveAs(xlssave);
ExcelWorkbook.Close(false);
invoke(Excel.Workbooks,'Open',xlssave);
Excel.visible = true;
sheet = 1;
data0 = {'Filename',input_name};
data1 = {'Parameter 1',param1};
data2 = {'Parameter 2',param2};
data3 = {'Parameter 3',param3};
data4 = {'Parameter 4',param4};
xlswrite1(xlssave,data0,sheet,'A1');
xlswrite1(xlssave,data1,sheet,'A2');
xlswrite1(xlssave,data2,sheet,'A3');
xlswrite1(xlssave,data3,sheet,'A4');
xlswrite1(xlssave,data4,sheet,'A5');
invoke(Excel.ActiveWorkbook,'Save');
Excel.Quit;
Excel.delete;
clear Excel;
end
But constantly I got the following error:
Error using Interface.Microsoft_Excel_15.0_Object_Library._Workbook/SaveAs
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Impossible d'enregistrer ce classeur sous le même nom qu'un autre classeur ou macro
complémentaire ouverts. Choisissez un nom différent, ou fermez l'autre classeur ou macro
complémentaire avant l'enregistrement.
Help File: xlmain11.chm
Help Context ID: 0
Error in xlswrite1 (line 128)
ExcelWorkbook.SaveAs(file);
The description is in french but says that it is impossible to save the workbook with the same name than another workbook or an opened macro and that I have to save the workbook with a different name or to close the existing file before saving. The problem is that I have no such file in the workspace or destination folder. Also, two excel files open with this syntax, one called with the correct name, but no data added in sheet 1, and a second one called "workbook2" also with no data.
If I change slightly the script to chose myself the file that need to be opened, then process and finally save, the xlswrite1 function works like a charm. With this script, if I remove the com server syntax and replace xlswrite1 by xlswrite, it works.
I'm using Matlab R2014b and Excel 2013.
Any idea what is wrong with xlswrite1 in the above code?
Best,
Guillaume

Risposte (1)

GMabilleau
GMabilleau il 18 Mag 2017
Nobody?

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by