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!