fprintf to write string to csv

5 views (last 30 days)
Vig
Vig on 5 Jun 2012
Hi,
I am trying to write some strings to a csv file using the fprintf. It's working good for some strings. However, for strings with commas, the code see the str_commas as a delimiter. Any suggestion. Thanks in advance.
eg. fprintf(WriteCellFile,'%s,%s,%s,%s,%s\r\n', Toxin,'k_fit','m_fit','Area','Curve')
%%Toxin is the string with commas.
Vig

Accepted Answer

Image Analyst
Image Analyst on 5 Jun 2012
You have to change the commas to something else (like "_comma_") if you want it to be a CSV file as you'd expect. You can use the strrep() function for this:
Toxin2 = strrep(Toxin, ',', '_comma_');
then write out Toxin2 instead of Toxin. Then at some point, you'll probably read in your csv back in and you'll need to undo that string replacement to decode it back to what it was when you started (i.e., return again to a normal comma):
originalString = strrep(readInString, '_comma_', ',');
Now you're ready to use the string again with the comma embedded in it.
  1 Comment
Vig
Vig on 5 Jun 2012
Awesome! This is a good precaution if I want to read the file back in as a csv format. Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!