Effacer les filtres
Effacer les filtres

Loop for end before it should

1 vue (au cours des 30 derniers jours)
Ali
Ali le 5 Oct 2022
Commenté : VBBV le 5 Oct 2022
hi,
I have in my function two for loops which are used to rename the sheets of an excel file. The first loop changes the name of the sheets that takes the values of a first list. The second loop changes the name of the sheets that takes the value of a second list. But sometimes the second loop end before changing all the names of the concerned sheets and sometimes it odes it perfectly i don't know why. Heres the code :
f = 1
g = 2
for i = 1:(length(nb)) %length(nb) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist1{1,nb(1,i)} ' Spectre tension'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist1{1,nb(1,i)} ' Impédence'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f =f+2 %represent odd sheets
g=g+2 %represent even sheets
end
for e = 1:(length(ne)) %length(nE) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist2{1,ne(1,e)} ' Spectre Courant'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist2{1,ne(1,e)} ' Spectre tension'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f=f+2
g=g+2
end
I don't understand why sometimes it work completly fine and others it doesn't finish it without any error codes. The first loop works perfectly fine and if there is only one loop weither the first or second it will also work fine.

Réponse acceptée

VBBV
VBBV le 5 Oct 2022
f = 1
g = 2
for i = 1:(length(nb)) %length(nb) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist1{1,nb(1,i)} ' Spectre tension'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist1{1,nb(1,i)} ' Impédence'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f =f+2
g=g+2
end
f = 1
g = 2
for e = 1:(length(ne)) %length(nE) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist2{1,ne(1,e)} ' Spectre Courant'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist2{1,ne(1,e)} ' Spectre tension'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f=f+2
g=g+2
end
  7 commentaires
Ali
Ali le 5 Oct 2022
@VBBV I didn't when i do that it will only ask me if i want to save over the old file and it stops the code from running even if click on yes
VBBV
VBBV le 5 Oct 2022
Ok. Then I think it's related to or something more to do with your other GUI application components.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits


Version

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by