Problem of writing a mixed type cell array to a csv file

4 views (last 30 days)
Hi! I have an issue regarding read/write on csv file. I have a large csv file containing mixed type of data(double and string). when I open the file using a text editor it is in the proper format and comma separated data. Also when I open it using excel(Excel 2013) it is displayed properly. Next,in my code, I read the data using xlsread() and choose some rows and assign them to a cell array. so the format looks similar to the initial file and I write the cell array to another csv file using xlswrite(). Now, when I open the file in excel it alerts that "the file may be corrupted. do you still want to open it?" but it is loaded and displayed properly. but when I open it using wordpad all it is shown is garbage values. Where is the problem? is there any write eqivalent to textscan so that I could write the file in a comma separated format?
Thanks

Accepted Answer

Stephen23
Stephen23 on 10 Sep 2014
If you know the format of your data, and are looking for something "similar" to textscan , try fprintf . It is slightly lower-level than textscan, but allows you to control how the text should be printed in your CSV file.
In fact you should navigate through MATLAB's help by using the list on the left hand side of the page: you will find all functions grouped into categories, with documentation, descriptions and examples. Take the time to browse this, and you might find something useful there!

More Answers (1)

Iain
Iain on 10 Sep 2014
If you want to write it as a csv file and if it must be a csv file and the data is really variable format, then you may have to literally go through the entire cell array turning all the numerical values into strings, and then write them to disk using fprintf.
If you want to do it in a slightly easier way, why not just use an excel file - and if you then need a csv file, you can always use excel to save it as a csv.
It depends how often you want to do it, but you can also use "actxserver" to make excel generate your csv with the formatting you want. This might be a better solution to manually doing it all.

Community Treasure Hunt

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

Start Hunting!