MATLAB Answers

xlswrite writing extra collumns

2 ビュー (過去 30 日間)
Mark Bodner
Mark Bodner 2020 年 8 月 12 日
コメント済み: Sudheer Bhimireddy 2020 年 8 月 12 日
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
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.

  1 件のコメント

Mark Bodner
Mark Bodner 2020 年 8 月 12 日
Thanks for the answer, but I'm not sure what file do mean to delete each time. Do you mean delete filename each time? I tried that and xlswrite still writes the extra columns. I also clear 'searchresult2' each iteration (it is alway correct on each iteration anyway with no extra columns), so I'm not sure what needs deleted?

サインインしてコメントする。


Sudheer Bhimireddy
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 件のコメント

Mark Bodner
Mark Bodner 2020 年 8 月 12 日
The IF statement is never true for more than one case in any iteration, and they never have the same searchresults (which is always cleared at the end of each iteration) and is recalculated after each iteration. So searchresult2 is always cleared at the end of each iteration, and recalculated before the next--indeed I checked it and searchresults is always new and correct each time, but the xlswrite for some reason writes the extra columns from the previous iteration and are coming from somewhere else involved in the xlswrite.
Sudheer Bhimireddy
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()

サインインしてコメントする。

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by