xlswrite writing extra collumns
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Walter Roberson
il 12 Ago 2020
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
il 12 Ago 2020
"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 Commenti
Sudheer Bhimireddy
il 12 Ago 2020
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()
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!