xlsWrite overwrites data despite setting a specific range
6 views (last 30 days)
Show older comments
Alicia Gascon Fernandez-Gubieda
on 3 Feb 2020
Answered: Walter Roberson
on 4 Feb 2020
I am trying to extrac data from Matlab into excell. I have a structure that contains several matrixes (these range from the size 1x600 to 1x800). I isolated each matrix in matlab (and called them m1, m2, m3...). But when I try to export them with xlswrite it seems to copy each matrix on top of the previous one, so instead of having an excell file with all of my matrixes, each one in a separate line, I have one single line showing the last matrix (m4 in this case).
I tried using '1:1', '2:2', etc, to write them in separate fields, but I still get the same error. Any suggestions? I have tried setting the range as 'A1:ZZ1', 'A2:ZZ2', but that didn't work either.
m1 = Structure(1).matrix; % Extracts the value of matrix of the of the first data point in structure
m2 = Structure(2).matrix; %
m3 = Structure(3).matrix;
m4 = Structure(4).matrix;
filename = 'Filename.csv';
xlswrite(filename,m1,'1:1');
xlswrite(filename,m2,'2:2');
xlswrite(filename,m3,'3:3');
xlswrite(filename,m4,'4:4');
2 Comments
dpb
on 3 Feb 2020
Edited: dpb
on 4 Feb 2020
filename = 'Filename.csv';
xlswrite(filename,m1,'1:1');
xlswrite(filename,m2,'2:2');
xlswrite(filename,m3,'3:3');
xlswrite(filename,m4,'4:4');
You're not writing an Excel file but a csv text file -- for which the 'Range' argument is ineffective as you've discovered because it is a sequential file.
Walter Roberson
on 3 Feb 2020
Also xlswrite will overwrite everything on Mac and Linux and on Windows that does not have Excel.
Accepted Answer
Walter Roberson
on 4 Feb 2020
When you use csv files, then all that can be done (without re-writing the entire file), is to append new rows on to the end of the file. It is not required that the number of columns in the new rows be the same as in previous rows.
If you want to add new rows to a csv file, then use dlmwrite() with the -append option.
I need to emphasize that this cannot be used to add new columns to a csv file.
If you need to add new columns to a csv file, you need to read in the data from the csv file, add on the new data, and write it out again.
To write a csv file, your options are xlswrite() [not recommended for csv], csvwrite() (it calls dlmwrite()), dlmwrite(), writetable(); as of R2019b there is also writematrix() and writecell()
If you want empty columns in a csv, then you will need to use writetable() (or writecell()). If I recall correctly, xlswrite() will not work for this purpose, not for csv files.
Or, of course, you could create your own writing routine using fopen(), fprintf() or fwrite(), and fclose().
csv files do not support "sheets". csv files do not support range arguments for xlswrite() or writetable()
csv files are text files, and there has only ever been one operating system that MATLAB ever supported (namely MVS) that could handle appending data onto the end of a line. Every other operating system that MATLAB has ever run on, text files have only ever been streams of bytes with specific bytes such as Newline to separate the lines, and the only way on those operating systems to add more data to a line has been to rewrite everything in the rest of the file.
This is not a MATLAB-imposed limitation: it is fundamental to the way that text files work in all operating systems in the last 20 years.
0 Comments
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!