Loop for end before it should

1 次查看(过去 30 天)
Ali
Ali 2022-10-5
评论: VBBV 2022-10-5
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.

采纳的回答

VBBV
VBBV 2022-10-5
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 个评论
Ali
Ali 2022-10-5
@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 2022-10-5
Ok. Then I think it's related to or something more to do with your other GUI application components.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by