xlswrite writing extra collumns
조회 수: 1 (최근 30일)
이전 댓글 표시
I have several xlswrite statements in my program within a loop as shown below. Even though after each iteration the table 'searchresult2' is cleared, the xlswrite statement seems to retain columns from previous iterations and writes them into empty columns at the end of subsequent files (e.g. columns of data from searchresult2 for filename15 get added into, and written into filename16. These extra columns from 15 are not even present in searchresult2 in 16, but still get written into 'PIN1617_ordered_3_16_from_15.xlsx'. Is there any way to avoid this? Thanks so much for any help.
for ...
.
.
.
if limit2==15
if gate15==0
filename15='PIN1617_ordered_3_15_from_14.xlsx';
xlswrite(filename15,searchresult2);
if basegate==1
filenameb15='PIN1617_ordered_3_15_from_14_base.xlsx';
xlswrite(filenameb15,searchbase);
basegate=0;
end
gate15=1;
end
end
if limit2==16
if gate16==0
filename16='PIN1617_ordered_3_16_from_15.xlsx';
xlswrite(filename16,searchresult2);
if basegate==1
filenameb16='PIN1617_ordered_3_16_from_15_base.xlsx';
xlswrite(filenameb16,searchbase);
basegate=0;
end
gate16=1;
end
end
.
.
.
clear searchresult2;
clear searchbase;
end
댓글 수: 0
답변 (2개)
Walter Roberson
2020년 8월 12일
Delete the file each time.
xlswrite() with no range specified is being treated as xlswrite() with a range just large enough to hold the to be written. Which in turn means that anything else that exists in the file is not to be removed.
Sudheer Bhimireddy
2020년 8월 12일
"e.g. columns of data from searchresult2 for filename15 get added into, and written into filename16"
That's because you dont have clear searchresult2 statement between the lines where filename15 and filename16 are being used. If the IF statement is true for both cases then filename15 and filename16 will have the same searhresult2
It would help if you can tell more, like, where are your calculating the searchresult2. Are your iterations based on limit2 variable?
댓글 수: 2
Sudheer Bhimireddy
2020년 8월 12일
Ok, just to debug the issue. Try the following:
- before the IF loop, print the size or values of searchresults2
- Inside the IF loop, print the same just before xlswrite()
- Finally, try to create a different filename, just to see if this is happening even while writing a new file.
- If you are using the latest version, try using writematrix(your_matrix,filename) instead of xlswrite()
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!