Loop for end before it should
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!